Compare commits
1 Commits
master
...
ch32v208br
| Author | SHA1 | Date |
|---|---|---|
|
|
843c1f3a2f |
|
|
@ -1,11 +0,0 @@
|
|||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
IndentAccessModifiers: false
|
||||
AccessModifierOffset: -4
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Left
|
||||
SortIncludes: CaseSensitive
|
||||
IndentPPDirectives: BeforeHash
|
||||
AlignAfterOpenBracket: BlockIndent
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
|
|
@ -2,12 +2,4 @@
|
|||
*.o
|
||||
*libmusl.a
|
||||
*liblwip.a
|
||||
.DS_Store
|
||||
|
||||
.cache/
|
||||
compile_commands.json
|
||||
.clangd
|
||||
Ubiquitous/XiZi_AIoT/services/app/bin/*
|
||||
Ubiquitous/XiZi_AIoT/build/*
|
||||
Ubiquitous/XiZi_AIoT/services/app/fs.img
|
||||
Ubiquitous/XiZi_AIoT/services/tools/mkfs/mkfs
|
||||
.DS_Store
|
||||
|
|
@ -1,92 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* XiUOS includes */
|
||||
#ifdef CONFIG_ADD_XIZI_FEATURES
|
||||
#include <xizi.h>
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ADD_NUTTX_FEATURES
|
||||
#include <nuttx/config.h>
|
||||
#endif
|
||||
|
||||
/* PikaPython includes */
|
||||
#include "pika_port.h"
|
||||
|
||||
/* External PikaPython application functions */
|
||||
extern int pika_app_init(void);
|
||||
extern int pika_app_run(void);
|
||||
extern int pika_app_deinit(void);
|
||||
extern int pika_app_is_initialized(void);
|
||||
extern int pika_app_load_script(const char* filename);
|
||||
|
||||
/* Test function for PikaPython */
|
||||
int test_pikapython(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
printf("\n=== PikaPython Test for XiUOS ===\n");
|
||||
|
||||
/* Initialize PikaPython runtime */
|
||||
printf("Initializing PikaPython...\n");
|
||||
ret = pika_app_init();
|
||||
if (ret != 0) {
|
||||
printf("Failed to initialize PikaPython: %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check if runtime is initialized */
|
||||
if (!pika_app_is_initialized()) {
|
||||
printf("PikaPython runtime not initialized\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("PikaPython runtime initialized successfully\n");
|
||||
|
||||
/* Run embedded Python script */
|
||||
printf("Running embedded Python script...\n");
|
||||
ret = pika_app_run();
|
||||
if (ret != 0) {
|
||||
printf("Failed to run Python script: %d\n", ret);
|
||||
pika_app_deinit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Test loading script from file (if file system is enabled) */
|
||||
#ifdef CONFIG_PIKAPYTHON_ENABLE_FILE_SYSTEM
|
||||
printf("Testing file system script loading...\n");
|
||||
ret = pika_app_load_script("/app/main.py");
|
||||
if (ret == 0) {
|
||||
printf("Script loaded from file successfully\n");
|
||||
ret = pika_app_run();
|
||||
if (ret != 0) {
|
||||
printf("Failed to run loaded script: %d\n", ret);
|
||||
}
|
||||
} else {
|
||||
printf("File system script loading not available or failed\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Deinitialize PikaPython runtime */
|
||||
printf("Deinitializing PikaPython...\n");
|
||||
ret = pika_app_deinit();
|
||||
if (ret != 0) {
|
||||
printf("Failed to deinitialize PikaPython: %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("PikaPython test completed successfully!\n");
|
||||
printf("=== End of PikaPython Test ===\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Main function for standalone test */
|
||||
#ifdef CONFIG_PIKAPYTHON_STANDALONE_TEST
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
printf("PikaPython Standalone Test\n");
|
||||
return test_pikapython();
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,15 +1,3 @@
|
|||
SRC_FILES := 4g_app.c
|
||||
|
||||
ifeq ($(CONFIG_DEVICE_ADL400),y)
|
||||
SRC_FILES += ch32v208_adl400.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEVICE_DTZ178),y)
|
||||
SRC_FILES += ch32v208_dtz178.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEVICE_DTSD342),y)
|
||||
SRC_FILES += ch32v208_dtsd342.c
|
||||
endif
|
||||
SRC_FILES := 4g_app.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
|||
|
|
@ -1,673 +0,0 @@
|
|||
/**
|
||||
* @file ch32v208_adl400.c
|
||||
* @brief ch32v208 board gets data from Acrel-ADL400 electricity meter with rs485 bus,
|
||||
* and then sends it to the server with 4G.
|
||||
* @author Huo Yujia (huoyujia081@126.com)
|
||||
* @version 1.0
|
||||
* @date 2024-07-10
|
||||
*/
|
||||
|
||||
#include <ModuleConfig.h>
|
||||
#include <adapter.h>
|
||||
#include <transform.h>
|
||||
#define MAX_FRAME_SIZE 256 // 最大帧大小
|
||||
#define MAX_BUFFER_SIZE 1024 * 2 // 最大缓冲区大小
|
||||
#define RECEIVE_DATA_INTERVAL_MS 1000 * 60 * 2 // ADL400数据采集间隔时间,单位为毫秒
|
||||
#define RESEND_COUNT 3 // 最大帧重发次数
|
||||
#define RECONNECT_COUNT 5 // 最大连接次数
|
||||
#define WATING_RESPONSE_MS 5000 // 等待响应时间,单位为毫秒
|
||||
|
||||
/**
|
||||
* @brief 生成Modbus RTU请求帧中的CRC循环冗余码
|
||||
* @param CRC_Ptr
|
||||
* @param LEN 需要生成CRC冗余码的数据长度
|
||||
* @return uint16_t 以小端顺序返回CRC循环冗余码
|
||||
*/
|
||||
static uint16_t generateCRC(uint8_t *CRC_Ptr, uint8_t LEN) {
|
||||
uint16_t CRC_Value = 0;
|
||||
uint8_t i = 0;
|
||||
uint8_t j = 0;
|
||||
|
||||
CRC_Value = 0xffff;
|
||||
for (i = 0; i < LEN; i++) // LEN为数组长度
|
||||
{
|
||||
CRC_Value ^= *(CRC_Ptr + i);
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (CRC_Value & 0x00001)
|
||||
CRC_Value = (CRC_Value >> 1) ^ 0xA001;
|
||||
else
|
||||
CRC_Value = (CRC_Value >> 1);
|
||||
}
|
||||
}
|
||||
CRC_Value = ((CRC_Value >> 8) + (CRC_Value << 8)); // 交换高低字节
|
||||
|
||||
return CRC_Value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 生成Modbus RTU请求帧,用于获取指定寄存器的值
|
||||
* @param address 请求的Modbus地址
|
||||
* @param functionCode 功能码
|
||||
* @param startAddress 数据起始地址
|
||||
* @param quantity 数据读取个数
|
||||
* @param modbusRtuRequestFrame 生成的ModBus RTU请求帧数组
|
||||
* @param requestFrameArrLength 生成的ModBus RTU请求帧数组长度,固定为8
|
||||
* @return int 0表示生成成功,其他结果表示生成失败
|
||||
* @note Modbus
|
||||
* RTU请求帧格式:地址(1字节)+功能码(1字节)+数据起始地址(2字节)+数据读取个数(2字节)+校验码(2字节)
|
||||
*/
|
||||
static int generateRequestFrame(unsigned char address, unsigned char functionCode, unsigned short startAddress, unsigned short quantity,
|
||||
unsigned char modbusRtuRequestFrame[], int requestFrameArrLength) {
|
||||
if (requestFrameArrLength != 8) {
|
||||
printf("the length of request frame array is not 8\n");
|
||||
return -1;
|
||||
}
|
||||
modbusRtuRequestFrame[0] = address;
|
||||
modbusRtuRequestFrame[1] = functionCode;
|
||||
modbusRtuRequestFrame[2] = (startAddress >> 8) & 0xff;
|
||||
modbusRtuRequestFrame[3] = startAddress & 0xff;
|
||||
modbusRtuRequestFrame[4] = (quantity >> 8) & 0xff;
|
||||
modbusRtuRequestFrame[5] = quantity & 0xff;
|
||||
modbusRtuRequestFrame[6] = (generateCRC(modbusRtuRequestFrame, 6) >> 8) & 0xff;
|
||||
modbusRtuRequestFrame[7] = generateCRC(modbusRtuRequestFrame, 6) & 0xff;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将要上传服务器的数据帧
|
||||
*/
|
||||
struct DataFrame {
|
||||
unsigned char id[13]; // 用响应的时间戳作为数据帧的id
|
||||
unsigned char data[MAX_FRAME_SIZE]; // 上传服务器的数据帧字符串,前12字节表示数据帧id。字符串格式:数据帧id,数据1,数据2,数据3...
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Modbus RTU响应数据帧的缓存,使用循环队列作为数据结构
|
||||
*/
|
||||
struct QueueBuffer {
|
||||
struct DataFrame *buffer[MAX_BUFFER_SIZE / sizeof(struct DataFrame)]; // 循环队列存储空间,使用数组存储
|
||||
int front; // 循环队列队头
|
||||
int rear; // 循环队列队尾
|
||||
pthread_mutex_t mutex; // 互斥访问循环队列信号量
|
||||
sem_t full; // 循环队列中有效成员个数的信号量
|
||||
};
|
||||
|
||||
#define BUFFER_ELEM_COUNT (MAX_BUFFER_SIZE / sizeof(struct DataFrame)) // 循环队列中可以容纳的最大成员个数
|
||||
|
||||
/**
|
||||
* @brief 初始化循环队列
|
||||
* @param pQueueBuffer 循环队列指针
|
||||
* @return * int 0表示初始化成功,其他表示初始化失败
|
||||
*/
|
||||
static int initBuffer(struct QueueBuffer *pQueueBuffer) {
|
||||
pQueueBuffer->front = 0;
|
||||
pQueueBuffer->rear = 0;
|
||||
if (PrivMutexCreate(&pQueueBuffer->mutex, 0) < 0) {
|
||||
printf("buffer mutex create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
if (PrivSemaphoreCreate(&pQueueBuffer->full, 0, 0) < 0) {
|
||||
printf("buffer full semaphore create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 循环队列入队,如果循环队列已满,则将最旧的成员出队后,新成员再入队
|
||||
* @param pQueueBuffer 循环队列指针
|
||||
* @param pDataFrame ADL400响应数据帧
|
||||
* @return int 0表示入队成功,其他表示入队失败
|
||||
*/
|
||||
static int offerBuffer(struct QueueBuffer *pQueueBuffer, struct DataFrame *pDataFrame) {
|
||||
/* 循环队列已满,将最旧的成员出队 */
|
||||
if ((pQueueBuffer->rear + 1) % BUFFER_ELEM_COUNT == pQueueBuffer->front) {
|
||||
struct DataFrame *frontDataFrame = pQueueBuffer->buffer[pQueueBuffer->front];
|
||||
PrivFree(frontDataFrame);
|
||||
pQueueBuffer->front = (pQueueBuffer->front + 1) % BUFFER_ELEM_COUNT;
|
||||
}
|
||||
/* 新成员入队 */
|
||||
pQueueBuffer->buffer[pQueueBuffer->rear] = pDataFrame;
|
||||
pQueueBuffer->rear = (pQueueBuffer->rear + 1) % BUFFER_ELEM_COUNT;
|
||||
printf("front: %d\n", pQueueBuffer->front);
|
||||
printf("rear: %d\n", pQueueBuffer->rear);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 循环队列出队,如果队列为空则返回NULL
|
||||
* @param pQueueBuffer 循环队列指针
|
||||
* @return struct DataFrame* 出队成员,如果队列为空则返回NULL
|
||||
*/
|
||||
static struct DataFrame *pollBuffer(struct QueueBuffer *pQueueBuffer) {
|
||||
/* 队列为空,返回NULL */
|
||||
if (pQueueBuffer->front == pQueueBuffer->rear) {
|
||||
return NULL;
|
||||
}
|
||||
/* 最旧的成员出队 */
|
||||
struct DataFrame *pFrontDataFrame = pQueueBuffer->buffer[pQueueBuffer->front];
|
||||
pQueueBuffer->buffer[pQueueBuffer->front] = NULL;
|
||||
pQueueBuffer->front = (pQueueBuffer->front + 1) % BUFFER_ELEM_COUNT;
|
||||
printf("front: %d\n", pQueueBuffer->front);
|
||||
printf("rear: %d\n", pQueueBuffer->rear);
|
||||
return pFrontDataFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 查看队头元素,如果队列为空则返回NULL
|
||||
* @param pQueueBuffer 循环队列指针
|
||||
* @return struct DataFrame* 队头元素,如果队列为空则返回NULL
|
||||
*/
|
||||
static struct DataFrame *peekBuffer(struct QueueBuffer *pQueueBuffer) {
|
||||
/* 如果队列为空,返回NULL */
|
||||
if (pQueueBuffer->front == pQueueBuffer->rear) {
|
||||
return NULL;
|
||||
}
|
||||
/* 返回队头元素,但不出队 */
|
||||
return pQueueBuffer->buffer[pQueueBuffer->front];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 改写PrivRead函数,原有函数只会读取接收缓冲区的当前已有字节,新函数会读取指定字节数再返回
|
||||
* @param fd 文件描述符
|
||||
* @param buf 数据读取到的位置
|
||||
* @param len 读取的指定字节数
|
||||
* @return int 如果读取到指定字节数返回0;如果到达WATING_RESPONSE_MS仍未读取到指定字节数,或者读数据错误,返回-1
|
||||
*/
|
||||
static int privReadEnoughData(int fd, void *buf, size_t len) {
|
||||
char *buffer = (char *)buf; // 将接收的存储空间指针强制转型
|
||||
int gottenBytes = 0; // 已经读取到的字节数
|
||||
int remainTime = WATING_RESPONSE_MS; // 剩余的时间
|
||||
|
||||
/* 只有接收的字节数不够,并且还有剩余时间,才可以继续读取 */
|
||||
while (gottenBytes < len && remainTime > 0) {
|
||||
int bytes = PrivRead(fd, buffer + gottenBytes, len - gottenBytes); // 从设备读取
|
||||
if (bytes > 0) {
|
||||
gottenBytes += bytes; // 读取到字节
|
||||
} else if (bytes < 0) {
|
||||
printf("Error reading from serial port\n");
|
||||
return -1; // 读取错误
|
||||
}
|
||||
PrivTaskDelay(100); // 每100ms读取一次
|
||||
remainTime -= 100; // 剩余时间减去100ms
|
||||
}
|
||||
/* 若没有剩余时间,表示还没有读取到指定的字节数,返回-1;若有剩余时间,表示已经读取了指定的字节数,返回0 */
|
||||
return remainTime < 0 ? -1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析ADL400响应的Modbus RTU数据帧
|
||||
* @param modbusRtuResponseFrame0 请求数据后ADL400的响应帧
|
||||
* @param modbusRtuResponseFrame1 请求时间戳后ADL400的响应帧
|
||||
* @param modbusRtuResponseFrame2 请求电流电压比后ADL400的响应帧
|
||||
* @param pDataFrame 将要上传给服务器的数据帧,包括id和数据
|
||||
*/
|
||||
static void parseModBusRtuResponseFrame(unsigned char *modbusRtuResponseFrame0, unsigned char *modbusRtuResponseFrame1,
|
||||
unsigned char *modbusRtuResponseFrame2, struct DataFrame *pDataFrame) {
|
||||
/* 从frame2中获取电压变比pt和电流变比ct */
|
||||
unsigned char *p = modbusRtuResponseFrame2;
|
||||
int pt = (unsigned short)modbusRtuResponseFrame2[3] << 8 | (unsigned short)modbusRtuResponseFrame2[4];
|
||||
int ct = (unsigned short)modbusRtuResponseFrame2[5] << 8 | (unsigned short)modbusRtuResponseFrame2[6];
|
||||
|
||||
/* 从frame1中获取时间戳,即数据帧的id */
|
||||
for (int i = 8; i >= 3; i--) {
|
||||
sprintf(pDataFrame->id, "%s%02x", pDataFrame->id, modbusRtuResponseFrame1[i]);
|
||||
sprintf(pDataFrame->data, "%s%02x", pDataFrame->data, modbusRtuResponseFrame1[i]);
|
||||
}
|
||||
|
||||
/* 从frame0中获取要上传到服务器的ADL400的数据 */
|
||||
for (int i = 3; i < modbusRtuResponseFrame0[2] + 3; i += 4) {
|
||||
int originalData = (unsigned int)modbusRtuResponseFrame0[i] << 24 | (unsigned int)modbusRtuResponseFrame0[i + 1] << 16 |
|
||||
(unsigned int)modbusRtuResponseFrame0[i + 2] << 8 | (unsigned int)modbusRtuResponseFrame0[i + 3];
|
||||
sprintf(pDataFrame->data, "%s,%d", pDataFrame->data, originalData); // 将数据拼接到字符串
|
||||
}
|
||||
|
||||
strcat(pDataFrame->data, "\n"); // 字符串末尾添加换行符,表示数据帧结束
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 从ADL400接收数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *receiveDataFromADL400Task(void *arg) {
|
||||
struct QueueBuffer *pQueueBuffer = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
int fd = PrivOpen("/dev/rs485_dev1", O_RDWR); // 打开设备文件
|
||||
if (fd < 0) { // 打开设备文件失败,打印错误信息
|
||||
printf("open rs485 fd error: %d\n", fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct SerialDataCfg rs485Configuration;
|
||||
memset(&rs485Configuration, 0, sizeof(struct SerialDataCfg));
|
||||
/* 读取RS485配置信息 */
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待
|
||||
int baudRatesOption = CFG->baudRate_Rs485;
|
||||
int dataBitsOption = CFG->dataBits_Rs485;
|
||||
int stopBitsOption = CFG->stopBits_Rs485;
|
||||
int parityOption = CFG->parity_Rs485;
|
||||
PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁
|
||||
switch (baudRatesOption) {
|
||||
case 1:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_2400;
|
||||
break;
|
||||
case 2:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_4800;
|
||||
break;
|
||||
case 3:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_9600;
|
||||
break;
|
||||
case 4:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_19200;
|
||||
break;
|
||||
case 5:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_38400;
|
||||
break;
|
||||
case 6:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_57600;
|
||||
break;
|
||||
case 7:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_115200;
|
||||
break;
|
||||
case 8:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_230400;
|
||||
break;
|
||||
default:
|
||||
rs485Configuration.serial_baud_rate = BAUD_RATE_9600;
|
||||
break;
|
||||
}
|
||||
switch (dataBitsOption) {
|
||||
case 1:
|
||||
rs485Configuration.serial_data_bits = DATA_BITS_8;
|
||||
break;
|
||||
case 2:
|
||||
rs485Configuration.serial_data_bits = DATA_BITS_9;
|
||||
break;
|
||||
default:
|
||||
rs485Configuration.serial_data_bits = DATA_BITS_8;
|
||||
break;
|
||||
}
|
||||
switch (stopBitsOption) {
|
||||
case 1:
|
||||
rs485Configuration.serial_stop_bits = STOP_BITS_1;
|
||||
break;
|
||||
case 2:
|
||||
rs485Configuration.serial_stop_bits = STOP_BITS_2;
|
||||
break;
|
||||
default:
|
||||
rs485Configuration.serial_stop_bits = STOP_BITS_1;
|
||||
break;
|
||||
}
|
||||
switch (parityOption) {
|
||||
case 1:
|
||||
rs485Configuration.serial_parity_mode = PARITY_NONE;
|
||||
break;
|
||||
case 2:
|
||||
rs485Configuration.serial_parity_mode = PARITY_ODD;
|
||||
break;
|
||||
case 3:
|
||||
rs485Configuration.serial_parity_mode = PARITY_EVEN;
|
||||
break;
|
||||
}
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = (void *)&rs485Configuration;
|
||||
if (0 != PrivIoctl(fd, OPE_INT, &ioctl_cfg)) {
|
||||
printf("ioctl uart fd error %d\n", fd);
|
||||
PrivClose(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned char modbusRtuRequestFrame[8]; // 定义Modbus RTU请求帧
|
||||
unsigned char modBusRtuResponseFrame0[256]; // 定义Modbus RTU响应帧0
|
||||
unsigned char modBusRtuResponseFrame1[256]; // 定义Modbus RTU响应帧1
|
||||
unsigned char modBusRtuResponseFrame2[32]; // 定义Modbus RTU响应帧2
|
||||
while (1) {
|
||||
/* 生成Modbus RTU请求帧0,用于请求ADL400数据 */
|
||||
if (generateRequestFrame(0x93, 0x03, 0x0000, 0x003C, modbusRtuRequestFrame, sizeof(modbusRtuRequestFrame)) < 0) {
|
||||
break; // 生成请求帧失败,退出循环
|
||||
}
|
||||
PrivWrite(fd, modbusRtuRequestFrame, sizeof(modbusRtuRequestFrame)); // 发送Modbus RTU请求帧
|
||||
/* 读取Modbus RTU响应帧0数据 */
|
||||
if (privReadEnoughData(fd, modBusRtuResponseFrame0, 5 + (0x003C << 1)) < 0) {
|
||||
printf("read data from adl400 time out\n"); // 读取超时,打印错误信息
|
||||
break; // 读取失败,退出循环
|
||||
}
|
||||
|
||||
/* 生成Modbus RTU请求帧1,用于请求时间戳 */
|
||||
if (generateRequestFrame(0x93, 0x03, 0x003C, 0x0003, modbusRtuRequestFrame, sizeof(modbusRtuRequestFrame)) < 0) {
|
||||
break; // 生成请求帧失败,退出循环
|
||||
}
|
||||
PrivWrite(fd, modbusRtuRequestFrame, sizeof(modbusRtuRequestFrame)); // 发送Modbus RTU请求帧
|
||||
/* 读取Modbus RTU响应帧1数据 */
|
||||
if (privReadEnoughData(fd, modBusRtuResponseFrame1, 5 + (0x0003 << 1)) < 0) {
|
||||
printf("read data from adl400 time out\n"); // 读取超时,打印错误信息
|
||||
break; // 读取失败,退出循环
|
||||
}
|
||||
|
||||
/* 生成Modbus RTU请求帧2,用于请求电压电流变比 */
|
||||
if (generateRequestFrame(0x93, 0x03, 0x008D, 0x0002, modbusRtuRequestFrame, sizeof(modbusRtuRequestFrame)) < 0) {
|
||||
break; // 生成请求帧失败,退出循环
|
||||
}
|
||||
PrivWrite(fd, modbusRtuRequestFrame, sizeof(modbusRtuRequestFrame)); // 发送Modbus RTU请求帧
|
||||
/* 读取Modbus RTU响应帧2 */
|
||||
if (privReadEnoughData(fd, modBusRtuResponseFrame2, 5 + (0x0002 << 1)) < 0) {
|
||||
printf("read data from adl400 time out\n"); // 读取超时,打印错误信息
|
||||
break; // 读取失败,退出循环
|
||||
}
|
||||
|
||||
/* 解析Modbus RTU响应帧 */
|
||||
struct DataFrame *pDataFrame = (struct DataFrame *)PrivMalloc(sizeof(struct DataFrame));
|
||||
memset(pDataFrame, 0, sizeof(struct DataFrame));
|
||||
parseModBusRtuResponseFrame(modBusRtuResponseFrame0, modBusRtuResponseFrame1, modBusRtuResponseFrame2, pDataFrame);
|
||||
|
||||
/* 将解析后的数据帧放入循环队列 */
|
||||
PrivMutexObtain(&pQueueBuffer->mutex); // 获取互斥锁
|
||||
offerBuffer(pQueueBuffer, pDataFrame); // 将数据帧放入队列
|
||||
printf("receive data from ADL400, id: %s\n", pDataFrame->id); // 打印接收到的数据帧ID
|
||||
PrivMutexAbandon(&pQueueBuffer->mutex); // 释放互斥锁
|
||||
PrivSemaphoreAbandon(&pQueueBuffer->full); // 释放信号量,即告知发送数据线程,队列中有新的数据帧
|
||||
|
||||
PrivTaskDelay(RECEIVE_DATA_INTERVAL_MS); // 延迟一段时间再读取下一帧数据
|
||||
}
|
||||
|
||||
PrivClose(fd); // 关闭设备文件
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过4G向服务器发送数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *sendDataToServerTask_4G(void *arg) {
|
||||
uint8_t serverIpAddress[16] = {}; // 目的IP地址
|
||||
uint8_t serverPort[6] = {}; // 目的端口号
|
||||
struct QueueBuffer *pQueueBuffer = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
unsigned char receiveBuffer[256]; // 从服务器接收每帧响应的存储空间
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); // 查找4G模块适配器
|
||||
|
||||
AdapterDeviceOpen(adapter); // 打开适配器对应的设备(实际打开串口中断)
|
||||
int baud_rate = BAUD_RATE_115200; // 波特率,用于设置4G模块串口
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate); // 对适配器对应设备进行配置(实际配置波特率)
|
||||
|
||||
struct DataFrame *pDataFrame = NULL; // 数据帧定义
|
||||
while (1) {
|
||||
PrivSemaphoreObtainWait(&pQueueBuffer->full, NULL); // 尝试获取循环队列队头元素,如果获取信号量失败,则等待信号量
|
||||
#ifdef BSP_BLE_CONFIG // 如果启用了BLE配置功能
|
||||
/* 获取互斥锁 */
|
||||
PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待
|
||||
|
||||
/* 尝试连接服务器 */
|
||||
sprintf(serverIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_4G[1],
|
||||
CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]);
|
||||
sprintf(serverPort, "%u", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_4G*-*-*-*\n");
|
||||
printf("serverIpAddress:\t%s\n", serverIpAddress);
|
||||
printf("serverPort:\t\t%s\n", serverPort);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
if (CFG->mqttSwitch_4G == 1) { // 如果使能MQTT
|
||||
AdapterDeviceMqttConnect(adapter, serverIpAddress, serverPort, CFG->mqttClientId_4G, CFG->mqttUsername_4G,
|
||||
CFG->mqttPassword_4G);
|
||||
} else { // 如果禁用MQTT
|
||||
AdapterDeviceConnect(adapter, CLIENT, serverIpAddress, serverPort, IPV4);
|
||||
}
|
||||
|
||||
AdapterDeviceNetstat(adapter); // 读取网络连接状态
|
||||
/* 若连接失败,则等待10s再次尝试连接 */
|
||||
if (CFG->mqttSwitch_4G == 0 && !adapter->network_info.is_connected ||
|
||||
CFG->mqttSwitch_4G == 1 && !adapter->network_info.mqttIsConnected) {
|
||||
PrivSemaphoreAbandon(&pQueueBuffer->full); // 释放信号量
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
printf("4G connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#else // 如果没有启用BLE配置功能
|
||||
/* 尝试连接到服务器 */
|
||||
sprintf(serverIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_4G[1],
|
||||
CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]);
|
||||
sprintf(serverPort, "%u", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_4G*-*-*-*\n");
|
||||
printf("serverIpAddress:\t%s\n", serverIpAddress);
|
||||
printf("serverPort:\t\t%s\n", serverPort);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int reconnectCount = RECONNECT_COUNT; // 尝试重新连接服务器最多RECONNECT_COUNT次
|
||||
while (reconnectCount > 0) {
|
||||
int res;
|
||||
if (CFG->mqttSwitch_4G == 1) {
|
||||
res = AdapterDeviceMqttConnect(adapter, mqttServerIp, mqttServerPort, CFG->mqttClientId_4G, CFG->mqttUsername_4G,
|
||||
CFG->mqttPassword_4G);
|
||||
} else {
|
||||
res = AdapterDeviceConnect(adapter, CLIENT, serverIpAddress, serverPort, IPV4);
|
||||
}
|
||||
if (res == 0) {
|
||||
break;
|
||||
}
|
||||
reconnectCount--;
|
||||
}
|
||||
if (reconnectCount <= 0) { // 若RECONNECT_COUNT次都连接失败,则等待10s再次尝试连接
|
||||
PrivSemaphoreAbandon(&pQueueBuffer->full); // 释放信号量
|
||||
printf("4G connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
PrivMutexObtain(&pQueueBuffer->mutex); // 获取互斥锁
|
||||
pDataFrame = pollBuffer(pQueueBuffer); // 从队列中获取数据帧
|
||||
PrivMutexAbandon(&pQueueBuffer->mutex); // 释放互斥锁
|
||||
|
||||
int resendCount = RESEND_COUNT; // 定义数据帧重发次数
|
||||
while (pDataFrame != NULL && resendCount > 0) { // 只有数据帧非空并且还有剩余重发次数,才进行发送
|
||||
/* 向服务器发送数据 */
|
||||
printf("pDataFrame->data: %s", pDataFrame->data);
|
||||
printf("send data to server, id: %s\n", pDataFrame->id);
|
||||
if (CFG->mqttSwitch_4G == 1) { // MQTT模式下,无需服务器响应数据
|
||||
AdapterDeviceMqttSend(adapter, CFG->mqttTopic_4G, pDataFrame->data,
|
||||
strlen(pDataFrame->data)); // 发送数据,注意当前最多发送256字节
|
||||
break;
|
||||
} else {
|
||||
AdapterDeviceSend(adapter, pDataFrame->data,
|
||||
strlen(pDataFrame->data)); // 发送数据,注意当前最多发送256字节
|
||||
|
||||
/* 从服务器接收响应,约定服务器接收完数据帧后,返回数据帧中的前12个字节,即数据帧id */
|
||||
/* 多读取2字节,是为了防止前面还有命令模式返回的剩余的\r\n影响判断 */
|
||||
memset(receiveBuffer, 0, sizeof(receiveBuffer));
|
||||
int receiveLength = AdapterDeviceRecv(adapter, receiveBuffer, strlen(pDataFrame->id) + 2);
|
||||
if (receiveLength == strlen(pDataFrame->id) + 2 || receiveLength == strlen(pDataFrame->id)) {
|
||||
/* 打印服务器响应 */
|
||||
printf("receiveLength: %d\n", receiveLength);
|
||||
printf("receiveBuffer: ");
|
||||
for (int i = 0; i < receiveLength; i++) {
|
||||
printf("%c", receiveBuffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
/* 比较服务器响应的内容与发送的数据帧id是否一致 */
|
||||
if (strstr(receiveBuffer, pDataFrame->id) != NULL) {
|
||||
break; // 接收成功,退出循环
|
||||
}
|
||||
} else {
|
||||
printf("receiveLength: %d\n", receiveLength);
|
||||
printf("receiveBuffer: ");
|
||||
for (int i = 0; i < receiveLength; i++) {
|
||||
printf("%d ", receiveBuffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
resendCount--;
|
||||
}
|
||||
if (pDataFrame != NULL) {
|
||||
PrivFree(pDataFrame); // 释放数据帧内存
|
||||
pDataFrame = NULL; // 避免野指针
|
||||
}
|
||||
// AdapterDeviceDisconnect(adapter, NULL); // 关闭适配器对应的设备
|
||||
#ifdef BSP_BLE_CONFIG
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
#endif
|
||||
if (resendCount <= 0) { // 如果数据帧重发次数超过上限,表示发送失败,丢弃该帧
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过以太网向服务器发送数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *sendDataToServerTask_Ethernet(void *arg) {
|
||||
uint8_t serverIpAddress[16] = {}; // 目的IP地址
|
||||
uint8_t serverPort[6] = {}; // 目的端口号
|
||||
struct QueueBuffer *pQueueBuffer = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
unsigned char receiveBuffer[256]; // 从服务器接收每帧响应的存储空间
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME); // 查找以太网模块适配器
|
||||
|
||||
#ifndef BSP_BLE_CONFIG // 如果没有使能蓝牙配置功能
|
||||
AdapterDeviceSetUp(adapter); // 启动以太网主任务线程
|
||||
AdapterDeviceSetDhcp(adapter, CFG->dhcpSwitch_Ethernet); // 启用或禁用DHCP
|
||||
#endif
|
||||
|
||||
struct DataFrame *pDataFrame = NULL; // 数据帧定义
|
||||
while (1) {
|
||||
PrivSemaphoreObtainWait(&pQueueBuffer->full, NULL); // 尝试获取循环队列队头元素,如果获取信号量失败,则等待信号量
|
||||
#ifdef BSP_BLE_CONFIG // 使能蓝牙配置功能
|
||||
/* 获取互斥锁 */
|
||||
PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待;
|
||||
|
||||
/* 尝试连接服务器 */
|
||||
sprintf(serverIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1],
|
||||
CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]);
|
||||
sprintf(serverPort, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_Ethernet*-*-*-*\n");
|
||||
printf("serverIpAddress:\t%s\n", serverIpAddress);
|
||||
printf("serverPort:\t\t%s\n", serverPort);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int res = AdapterDeviceConnect(adapter, CLIENT, serverIpAddress, serverPort, IPV4);
|
||||
|
||||
/* 连接失败,则等待10s再次尝试连接 */
|
||||
if (res != 0 && res != 0x1D) {
|
||||
PrivSemaphoreAbandon(&pQueueBuffer->full); // 释放信号量
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
printf("Ethernet connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
/* 尝试连接到服务器 */
|
||||
sprintf(serverIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1],
|
||||
CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]);
|
||||
sprintf(serverPort, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_Ethernet*-*-*-*\n");
|
||||
printf("serverIpAddress:\t%s\n", serverIpAddress);
|
||||
printf("serverPort:\t\t%s\n", serverPort);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int reconnectCount = RECONNECT_COUNT; // 尝试重新连接服务器最多RECONNECT_COUNT次
|
||||
while (reconnectCount > 0) {
|
||||
int res = AdapterDeviceConnect(adapter, CLIENT, serverIpAddress, serverPort, IPV4); // 尝试连接服务器
|
||||
if (res == 0 || res == 0x1D) {
|
||||
break;
|
||||
}
|
||||
reconnectCount--;
|
||||
}
|
||||
if (reconnectCount <= 0) { // 若RECONNECT_COUNT次都连接失败,则等待10s再次尝试连接
|
||||
PrivSemaphoreAbandon(&pQueueBuffer->full); // 释放信号量
|
||||
printf("Ethernet connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
PrivMutexObtain(&pQueueBuffer->mutex); // 获取互斥锁
|
||||
pDataFrame = pollBuffer(pQueueBuffer); // 从队列中获取数据帧
|
||||
PrivMutexAbandon(&pQueueBuffer->mutex); // 释放互斥锁
|
||||
|
||||
int resendCount = RESEND_COUNT; // 定义数据帧重发次数
|
||||
|
||||
/* 只有数据帧非空并且还有剩余重发次数,才进行发送 */
|
||||
while (pDataFrame != NULL && resendCount > 0) {
|
||||
/* 向服务器发送数据 */
|
||||
printf("send data to server, id: %s\n", pDataFrame->id);
|
||||
printf("pDataFrame->data: %s", pDataFrame->data);
|
||||
AdapterDeviceSend(adapter, pDataFrame->data,
|
||||
strlen(pDataFrame->data)); // 发送数据,注意当前最多发送256字节
|
||||
|
||||
/* 从服务器接收响应,约定服务器接收完数据帧后,返回数据帧中的前12个字节,即数据帧id */
|
||||
memset(receiveBuffer, 0, sizeof(receiveBuffer));
|
||||
PrivTaskDelay(6000);
|
||||
if (AdapterDeviceRecv(adapter, receiveBuffer, strlen(pDataFrame->id)) == strlen(pDataFrame->id)) {
|
||||
/* 打印服务器响应 */
|
||||
printf("receiveBuffer: ");
|
||||
for (int i = 0; i < strlen(receiveBuffer); i++) {
|
||||
printf("%c", receiveBuffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
/* 比较服务器响应的内容与发送的数据帧id是否一致 */
|
||||
if (strstr(pDataFrame->id, receiveBuffer) != NULL) {
|
||||
break; // 接收成功,退出循环
|
||||
}
|
||||
}
|
||||
resendCount--;
|
||||
}
|
||||
if (pDataFrame != NULL) {
|
||||
PrivFree(pDataFrame); // 释放数据帧内存
|
||||
pDataFrame = NULL; // 避免野指针
|
||||
}
|
||||
AdapterDeviceDisconnect(adapter, NULL);
|
||||
#ifdef BSP_BLE_CONFIG
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
#endif
|
||||
if (resendCount <= 0) { // 如果数据帧重发次数超过上限,表示发送失败,丢弃该帧
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 开启从ADL400接收数据的线程以及上传数据到服务器的线程,此方法在main方法中被调用,开机或复位启动
|
||||
*/
|
||||
void startUpTransformDataTask(void) {
|
||||
/* 分配循环队列空间 */
|
||||
struct QueueBuffer *pQueueBuffer = (struct QueueBuffer *)PrivCalloc(1, sizeof(struct QueueBuffer));
|
||||
if (initBuffer(pQueueBuffer) < 0) {
|
||||
PrivFree(pQueueBuffer);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 启动从ADL400接收数据的线程 */
|
||||
pthread_attr_t receiveDataFromADL400TaskAttr;
|
||||
pthread_args_t receiveDataFromADL400TaskArgs;
|
||||
receiveDataFromADL400TaskAttr.schedparam.sched_priority = 16; // 线程优先级
|
||||
receiveDataFromADL400TaskAttr.stacksize = 2048; // 线程栈大小
|
||||
receiveDataFromADL400TaskArgs.pthread_name = "receiveDataFromADL400Task"; // 线程名字
|
||||
receiveDataFromADL400TaskArgs.arg = pQueueBuffer; // 线程参数
|
||||
pthread_t receiveDataThread; // 线程ID
|
||||
PrivTaskCreate(&receiveDataThread, &receiveDataFromADL400TaskAttr, receiveDataFromADL400Task, &receiveDataFromADL400TaskArgs);
|
||||
PrivTaskStartup(&receiveDataThread);
|
||||
|
||||
/* 启动上传数据到服务器的线程 */
|
||||
pthread_attr_t sendDataToServerTaskAttr;
|
||||
pthread_args_t sendDataToServerTaskArgs;
|
||||
sendDataToServerTaskAttr.schedparam.sched_priority = 16; // 线程优先级
|
||||
sendDataToServerTaskAttr.stacksize = 2200; // 线程栈大小
|
||||
sendDataToServerTaskArgs.pthread_name = "sendDataToServerTask"; // 线程名字
|
||||
sendDataToServerTaskArgs.arg = pQueueBuffer; // 线程参数
|
||||
pthread_t sendDataThread; // 线程ID
|
||||
void *(*start_routine)(void *) = sendDataToServerTask_4G; // 通过4G模块上传到服务器
|
||||
// void *(*start_routine)(void *) = sendDataToServerTask_Ethernet; // 通过以太网模块上传到服务器
|
||||
PrivTaskCreate(&sendDataThread, &sendDataToServerTaskAttr, start_routine, &sendDataToServerTaskArgs); // 通过4G模块上传到服务器
|
||||
PrivTaskStartup(&sendDataThread);
|
||||
}
|
||||
|
|
@ -1,807 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file .c
|
||||
* @brief Support reading data from WASION DTSD342 using the Modbus-RTU protocol
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2025.4.21
|
||||
*/
|
||||
|
||||
#include <ModuleConfig.h>
|
||||
#include <adapter.h>
|
||||
#include <transform.h>
|
||||
#include <math.h>
|
||||
#include <cJSON.h>
|
||||
|
||||
#define MAX_FRAME_SIZE 256 // 最大帧大小
|
||||
#define MAX_DATA_SIZE 1024 // 最大数据大小
|
||||
#define MAX_BUFFER_SIZE 1024 * 2 // 最大缓冲区大小
|
||||
#define RECEIVE_DATA_INTERVAL_MS 1000 * 60 * 2 // DTSD342数据采集间隔时间,单位为毫秒
|
||||
#define SAMPLE_DATA_INTERVAL_MS 350 // 稳定连续采集的间隔时间,单位为毫秒
|
||||
#define SEND_FRAME_LEN 8 // 发送帧长度
|
||||
#define RECEIVE_FRAME_LEN 5 // 返回帧长度(不包含各项数据长度)
|
||||
#define RESEND_COUNT 3 // 最大帧重发次数
|
||||
#define RECONNECT_COUNT 5 // 最大连接次数
|
||||
#define WATING_RESPONSE_MS 5000 // 等待响应时间,单位为毫秒
|
||||
#define DATA_COUNT (sizeof(data_start_address_map) / sizeof(data_start_address_map[0])) // 数据项数量
|
||||
#define READ_COMMAND 0x03 /* 读取数据 */
|
||||
#define ADDRESS 0x01
|
||||
#define FRAME_ID "DTSD342"
|
||||
|
||||
#ifndef DATA_ITEMS_DEF_H
|
||||
#define DATA_ITEMS_DEF_H
|
||||
|
||||
#define DATA_ITEMS_XMACRO \
|
||||
X(FORWARD_ACTIVE_ENERGY, 0x2006, 4, 1) /* 正向有功电能,4字节,1位小数,单位Wh */ \
|
||||
X(FORWARD_REACTIVE_ENERGY, 0x200E, 4, 1) /* 正向无功电能,4字节,1位小数,单位varh */ \
|
||||
X(REVERSE_ACTIVE_ENERGY, 0x2106, 4, 1) /* 反向有功电能,4字节,1位小数,单位Wh */ \
|
||||
X(REVERSE_REACTIVE_ENERGY, 0x210E, 4, 1) /* 反向无功电能,4字节,1位小数,单位varh */ \
|
||||
X(VOLTAGE_A, 0x1800, 4, 3) /* A相电压,4字节,3位小数,单位V */ \
|
||||
X(VOLTAGE_B, 0x1802, 4, 3) /* B相电压,4字节,3位小数,单位V */ \
|
||||
X(VOLTAGE_C, 0x1804, 4, 3) /* C相电压,4字节,3位小数,单位V */ \
|
||||
X(CURRENT_A, 0x1810, 4, 4) /* A相电流,4字节,4位小数,单位A */ \
|
||||
X(CURRENT_B, 0x1812, 4, 4) /* B相电流,4字节,4位小数,单位A */ \
|
||||
X(CURRENT_C, 0x1814, 4, 4) /* C相电流,4字节,4位小数,单位A */ \
|
||||
X(ACTIVE_POWER_A, 0x181A, 4, 1) /* A有功功率,4字节,1位小数,单位W */ \
|
||||
X(ACTIVE_POWER_B, 0x181C, 4, 1) /* B有功功率,4字节,1位小数,单位W */ \
|
||||
X(ACTIVE_POWER_C, 0x181E, 4, 1) /* C有功功率,4字节,1位小数,单位W */ \
|
||||
X(ACTIVE_POWER_TOTAL, 0x1820, 4, 1) /* 总有功功率,4字节,1位小数,单位W */ \
|
||||
X(REACTIVE_POWER_A, 0x1822, 4, 1) /* A无功功率,4字节,1位小数,单位var */ \
|
||||
X(REACTIVE_POWER_B, 0x1824, 4, 1) /* B无功功率,4字节,1位小数,单位var */ \
|
||||
X(REACTIVE_POWER_C, 0x1826, 4, 1) /* C无功功率,4字节,1位小数,单位var */ \
|
||||
X(REACTIVE_POWER_TOTAL, 0x1828, 4, 1) /* 总无功功率,4字节,1位小数,单位var */ \
|
||||
X(APPARENT_POWER_A, 0x182A, 4, 1) /* A视在功率,4字节,1位小数,单位VA */ \
|
||||
X(APPARENT_POWER_B, 0x182C, 4, 1) /* B视在功率,4字节,1位小数,单位VA */ \
|
||||
X(APPARENT_POWER_C, 0x182E, 4, 1) /* C视在功率,4字节,1位小数,单位VA */ \
|
||||
X(APPARENT_POWER_TOTAL, 0x1830, 4, 1) /* 总视在功率,4字节,1位小数,单位VA */ \
|
||||
X(POWER_FACTOR_A, 0x1019, 2, 3) /* A功率因数,2字节,3位小数,单位无 */ \
|
||||
X(POWER_FACTOR_B, 0x101A, 2, 3) /* B功率因数,2字节,3位小数,单位无 */ \
|
||||
X(POWER_FACTOR_C, 0x101B, 2, 3) /* C功率因数,2字节,3位小数,单位无 */ \
|
||||
X(POWER_FACTOR_TOTAL, 0x101C, 2, 3) /* 总功率因数,2字节,3位小数,单位无 */ \
|
||||
X(FREQUENCY, 0x101D, 2, 2) /* 电网频率,2字节,2位小数,单位Hz */ \
|
||||
X(INTERNAL_TEMPERATURE, 0x0135, 2, 0) /* 内部温度,2字节,0位小数,单位℃ */
|
||||
|
||||
#endif // DATA_ITEMS_DEF_H
|
||||
|
||||
#ifndef DATA_ITEMS_H
|
||||
#define DATA_ITEMS_H
|
||||
|
||||
typedef enum {
|
||||
#define X(name, start_address, size, dec) name,
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
} DataIdIndex;
|
||||
|
||||
typedef struct {
|
||||
uint8_t byte_size;
|
||||
uint8_t decimal_places;
|
||||
} DataInfo;
|
||||
|
||||
static const uint16_t data_start_address_map[] = {
|
||||
#define X(name, start_address, size, dec) start_address,
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
};
|
||||
|
||||
static const DataInfo data_info_map[] = {
|
||||
#define X(name, start_address, size, dec) {size, dec},
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
};
|
||||
|
||||
static const char *data_id_names[] = {
|
||||
#define X(name, start_address, size, dec) #name,
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
};
|
||||
|
||||
#endif // DATA_ITEMS_H
|
||||
|
||||
/**
|
||||
* @brief 生成Modbus RTU请求帧中的CRC循环冗余码
|
||||
* @param data 需要生成CRC冗余码的数据指针
|
||||
* @param len 需要生成CRC冗余码的数据长度
|
||||
* @return uint16_t
|
||||
*/
|
||||
static uint16_t GenerateCRC(uint8_t *data, uint8_t len) {
|
||||
uint16_t crc = 0xFFFF;
|
||||
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
crc ^= data[i];
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
if (crc & 0x0001)
|
||||
crc = (crc >> 1) ^ 0xA001;
|
||||
else
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return crc; // 返回原始值,高字节在高位
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 生成Modbus RTU请求帧,用于获取指定寄存器的值
|
||||
* @param address 请求的Modbus地址
|
||||
* @param function_code 功能码
|
||||
* @param start_address 数据起始地址
|
||||
* @param quantity 数据读取个数
|
||||
* @param request_frame 生成的ModBus RTU请求帧数组
|
||||
* @return int 0表示生成成功,其他结果表示生成失败
|
||||
* @note Modbus
|
||||
* RTU请求帧格式:地址(1字节)+功能码(1字节)+数据起始地址(2字节)+数据读取个数(2字节)+校验码(2字节)
|
||||
*/
|
||||
static int GenerateRequestFrame(unsigned char address, unsigned char function_code, unsigned short start_address, unsigned short quantity, unsigned char request_frame[]) {
|
||||
request_frame[0] = address;
|
||||
request_frame[1] = function_code;
|
||||
request_frame[2] = (start_address >> 8) & 0xff;
|
||||
request_frame[3] = start_address & 0xff;
|
||||
request_frame[4] = (quantity >> 8) & 0xff;
|
||||
request_frame[5] = quantity & 0xff;
|
||||
|
||||
uint16_t crc = GenerateCRC(request_frame, 6);
|
||||
|
||||
request_frame[6] = crc & 0xff;
|
||||
request_frame[7] = (crc >> 8) & 0xff;
|
||||
|
||||
// printf("GenerateRequestFrame: ");
|
||||
// for (int i = 0; i < 8; i++)
|
||||
// printf("%02X ", request_frame[i]);
|
||||
// printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将要上传服务器的数据帧
|
||||
*/
|
||||
struct DataFrame {
|
||||
unsigned char id[8]; // 电表型号标识
|
||||
unsigned char data[MAX_DATA_SIZE]; // 上传服务器的数据帧字符串,用JSON封装
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Modbus RTU响应数据帧的缓存,使用循环队列作为数据结构
|
||||
*/
|
||||
struct QueueBuffer {
|
||||
struct DataFrame *buffer[MAX_BUFFER_SIZE / sizeof(struct DataFrame)]; // 循环队列存储空间,使用数组存储
|
||||
int front; // 循环队列队头
|
||||
int rear; // 循环队列队尾
|
||||
pthread_mutex_t mutex; // 互斥访问循环队列信号量
|
||||
sem_t full; // 循环队列中有效成员个数的信号量
|
||||
};
|
||||
|
||||
#define BUFFER_ELEM_COUNT (MAX_BUFFER_SIZE / sizeof(struct DataFrame)) // 循环队列中可以容纳的最大成员个数
|
||||
|
||||
/**
|
||||
* @brief 初始化循环队列
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @return * int 0表示初始化成功,其他表示初始化失败
|
||||
*/
|
||||
static int InitBuffer(struct QueueBuffer *queue_buffer_ptr) {
|
||||
queue_buffer_ptr->front = 0;
|
||||
queue_buffer_ptr->rear = 0;
|
||||
if (PrivMutexCreate(&queue_buffer_ptr->mutex, 0) < 0) {
|
||||
printf("buffer mutex create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
if (PrivSemaphoreCreate(&queue_buffer_ptr->full, 0, 0) < 0) {
|
||||
printf("buffer full semaphore create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 循环队列入队,如果循环队列已满,则将最旧的成员出队后,新成员再入队
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @param data_frame_ptr DTSD342响应数据帧
|
||||
* @return int 0表示入队成功,其他表示入队失败
|
||||
*/
|
||||
static int OfferBuffer(struct QueueBuffer *queue_buffer_ptr, struct DataFrame *data_frame_ptr) {
|
||||
/* 循环队列已满,将最旧的成员出队 */
|
||||
if ((queue_buffer_ptr->rear + 1) % BUFFER_ELEM_COUNT == queue_buffer_ptr->front) {
|
||||
struct DataFrame *front_data_frame_ptr = queue_buffer_ptr->buffer[queue_buffer_ptr->front];
|
||||
PrivFree(front_data_frame_ptr);
|
||||
queue_buffer_ptr->front = (queue_buffer_ptr->front + 1) % BUFFER_ELEM_COUNT;
|
||||
}
|
||||
/* 新成员入队 */
|
||||
queue_buffer_ptr->buffer[queue_buffer_ptr->rear] = data_frame_ptr;
|
||||
queue_buffer_ptr->rear = (queue_buffer_ptr->rear + 1) % BUFFER_ELEM_COUNT;
|
||||
printf("front: %d\n", queue_buffer_ptr->front);
|
||||
printf("rear: %d\n", queue_buffer_ptr->rear);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 循环队列出队,如果队列为空则返回NULL
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @return struct DataFrame* 出队成员,如果队列为空则返回NULL
|
||||
*/
|
||||
static struct DataFrame *PollBuffer(struct QueueBuffer *queue_buffer_ptr) {
|
||||
/* 队列为空,返回NULL */
|
||||
if (queue_buffer_ptr->front == queue_buffer_ptr->rear) {
|
||||
return NULL;
|
||||
}
|
||||
/* 最旧的成员出队 */
|
||||
struct DataFrame *front_data_frame_ptr = queue_buffer_ptr->buffer[queue_buffer_ptr->front];
|
||||
queue_buffer_ptr->buffer[queue_buffer_ptr->front] = NULL;
|
||||
queue_buffer_ptr->front = (queue_buffer_ptr->front + 1) % BUFFER_ELEM_COUNT;
|
||||
printf("front: %d\n", queue_buffer_ptr->front);
|
||||
printf("rear: %d\n", queue_buffer_ptr->rear);
|
||||
return front_data_frame_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 查看队头元素,如果队列为空则返回NULL
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @return struct DataFrame* 队头元素,如果队列为空则返回NULL
|
||||
*/
|
||||
static struct DataFrame *PeekBuffer(struct QueueBuffer *queue_buffer_ptr) {
|
||||
/* 如果队列为空,返回NULL */
|
||||
if (queue_buffer_ptr->front == queue_buffer_ptr->rear) {
|
||||
return NULL;
|
||||
}
|
||||
/* 返回队头元素,但不出队 */
|
||||
return queue_buffer_ptr->buffer[queue_buffer_ptr->front];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 改写PrivRead函数,原有函数只会读取接收缓冲区的当前已有字节,新函数会读取指定字节数再返回
|
||||
* @param fd 文件描述符
|
||||
* @param buf 数据读取到的位置
|
||||
* @param len 读取的指定字节数
|
||||
* @return int 如果读取到指定字节数返回0;如果到达WATING_RESPONSE_MS仍未读取到指定字节数,或者读数据错误,返回-1
|
||||
*/
|
||||
static int PrivReadEnoughData(int fd, void *buf, size_t len) {
|
||||
char *buffer = (char *)buf; // 将接收的存储空间指针强制转型
|
||||
int gotten_bytes = 0; // 已经读取到的字节数
|
||||
int remain_time = WATING_RESPONSE_MS; // 剩余的时间
|
||||
|
||||
/* 只有接收的字节数不够,并且还有剩余时间,才可以继续读取 */
|
||||
while (gotten_bytes < len && remain_time > 0) {
|
||||
int bytes = PrivRead(fd, buffer + gotten_bytes, len - gotten_bytes); // 从设备读取
|
||||
// printf("gotten_bytes: %d\n", bytes);
|
||||
gotten_bytes += bytes;
|
||||
PrivTaskDelay(100); // 每100ms读取一次
|
||||
remain_time -= 100; // 剩余时间减去100ms
|
||||
}
|
||||
/* 若没有剩余时间,表示还没有读取到指定的字节数,返回-1;若有剩余时间,表示已经读取了指定的字节数,返回0 */
|
||||
return remain_time < 0 ? -1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析DTSD342响应的Modbus RTU数据帧
|
||||
* @param response_frame 返回的数据帧
|
||||
* @param data_info_ptr 数据项的字节数和小数位数信息
|
||||
* @param item_id 数据项ID
|
||||
* @param root 根节点
|
||||
*/
|
||||
static void ParseResponseFrame(unsigned char *response_frame, const DataInfo *data_info_ptr, int item_id, cJSON *root) {
|
||||
if (!response_frame || !data_info_ptr || !root) {
|
||||
printf("Invalid input to ParseResponseFrame\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t data_len = response_frame[2]; // 数据域长度
|
||||
if (data_len != data_info_ptr->byte_size) {
|
||||
printf("Invalid data length in frame\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *data_value = &response_frame[3]; // 数据内容起始位置
|
||||
|
||||
// for (int i = 0; i < data_info_ptr->byte_size; i++)
|
||||
// printf("%02x ", data_value[i]);
|
||||
|
||||
uint32_t raw_value = 0;
|
||||
for (int i = 0; i < data_len; i++) {
|
||||
raw_value = (raw_value << 8) | data_value[i]; // 高字节在前
|
||||
}
|
||||
|
||||
double scaled_value = raw_value / pow(10, data_info_ptr->decimal_places);
|
||||
// printf("Parsed value: %.*f\n", data_info_ptr->decimal_places, scaled_value);
|
||||
|
||||
char format[10];
|
||||
snprintf(format, sizeof(format), "%%.%df", data_info_ptr->decimal_places);
|
||||
|
||||
char scaled_value_str[20];
|
||||
snprintf(scaled_value_str, sizeof(scaled_value_str), format, scaled_value);
|
||||
|
||||
cJSON_AddStringToObject(root, data_id_names[item_id], scaled_value_str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 从DTSD342接收数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *ReceiveDataFromDTSD342Task(void *arg) {
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
int fd = PrivOpen("/dev/rs485_dev1", O_RDWR); // 打开设备文件
|
||||
if (fd < 0) { // 打开设备文件失败,打印错误信息
|
||||
printf("open rs485 fd error: %d\n", fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct SerialDataCfg rs485_configuration;
|
||||
memset(&rs485_configuration, 0, sizeof(struct SerialDataCfg));
|
||||
/* 读取RS485配置信息 */
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待
|
||||
int baud_rates_option = CFG->baudRate_Rs485;
|
||||
int data_bits_option = CFG->dataBits_Rs485;
|
||||
int stop_bits_option = CFG->stopBits_Rs485;
|
||||
int parity_option = CFG->parity_Rs485;
|
||||
PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁
|
||||
switch (baud_rates_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_2400;
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_4800;
|
||||
break;
|
||||
case 3:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_9600; // 默认波特率9600
|
||||
break;
|
||||
case 4:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_19200;
|
||||
break;
|
||||
case 5:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_38400;
|
||||
break;
|
||||
case 6:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_57600;
|
||||
break;
|
||||
case 7:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_115200;
|
||||
break;
|
||||
case 8:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_230400;
|
||||
break;
|
||||
default:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_9600;
|
||||
break;
|
||||
}
|
||||
switch (data_bits_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_data_bits = DATA_BITS_8;
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_data_bits = DATA_BITS_9;
|
||||
break;
|
||||
default:
|
||||
rs485_configuration.serial_data_bits = DATA_BITS_8;
|
||||
break;
|
||||
}
|
||||
switch (stop_bits_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_stop_bits = STOP_BITS_1;
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_stop_bits = STOP_BITS_2;
|
||||
break;
|
||||
default:
|
||||
rs485_configuration.serial_stop_bits = STOP_BITS_1;
|
||||
break;
|
||||
}
|
||||
switch (parity_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_parity_mode = PARITY_NONE; // 默认无奇偶校验
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_parity_mode = PARITY_ODD;
|
||||
break;
|
||||
case 3:
|
||||
rs485_configuration.serial_parity_mode = PARITY_EVEN;
|
||||
break;
|
||||
}
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = (void *)&rs485_configuration;
|
||||
if (0 != PrivIoctl(fd, OPE_INT, &ioctl_cfg)) {
|
||||
printf("ioctl uart fd error %d\n", fd);
|
||||
PrivClose(fd);
|
||||
return NULL;
|
||||
}
|
||||
printf("open rs485 fd success %d\n", fd);
|
||||
|
||||
unsigned char request_frame[MAX_FRAME_SIZE]; // 定义请求帧
|
||||
unsigned char response_frame[MAX_FRAME_SIZE]; // 定义回复帧
|
||||
while (1) {
|
||||
printf("enter cycle\n");
|
||||
struct DataFrame *data_frame_ptr = (struct DataFrame *)PrivMalloc(sizeof(struct DataFrame));
|
||||
memset(data_frame_ptr, 0, sizeof(struct DataFrame));
|
||||
|
||||
// 创建一个空的JSON对象
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
int is_success = 1;
|
||||
|
||||
for (int i = 0; i < DATA_COUNT; i++) {
|
||||
const uint16_t start_address = data_start_address_map[i];
|
||||
const DataInfo *data_info = &data_info_map[i];
|
||||
|
||||
memset(request_frame, 0, sizeof(request_frame));
|
||||
if (GenerateRequestFrame(ADDRESS, READ_COMMAND, start_address, data_info->byte_size / 2, request_frame) < 0) {
|
||||
printf("Generate frame failed for index %d\n", i);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
PrivWrite(fd, request_frame, SEND_FRAME_LEN); // 发送Modbus RTU请求帧
|
||||
|
||||
/* 读取响应帧数据 */
|
||||
memset(response_frame, 0, sizeof(response_frame));
|
||||
if (PrivReadEnoughData(fd, response_frame, RECEIVE_FRAME_LEN + data_info->byte_size) < 0) {
|
||||
printf("Timeout reading response for index %d\n", i);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// printf("Response frame for index %d: ", i);
|
||||
// for (int j = 0; j < RECEIVE_FRAME_LEN + data_info->byte_size; j++)
|
||||
// printf("%02X ", response_frame[j]);
|
||||
// printf("\n");
|
||||
|
||||
// 校验帧头、帧尾
|
||||
if (response_frame[0] != ADDRESS || response_frame[1] != READ_COMMAND) {
|
||||
printf("Invalid frame format for index %d\n", i);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// 校验和
|
||||
uint16_t calc_crc16 = GenerateCRC(response_frame, RECEIVE_FRAME_LEN + data_info->byte_size - 2);
|
||||
uint16_t recv_crc16 = (response_frame[RECEIVE_FRAME_LEN + data_info->byte_size - 1] << 8) | response_frame[RECEIVE_FRAME_LEN + data_info->byte_size - 2];
|
||||
if (calc_crc16 != recv_crc16) {
|
||||
printf("Modbus CRC16 error at index %d: calc %04X, recv %04X\n", i, calc_crc16, recv_crc16);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
ParseResponseFrame(response_frame, data_info, i, root);
|
||||
|
||||
PrivTaskDelay(SAMPLE_DATA_INTERVAL_MS);
|
||||
}
|
||||
|
||||
if (!is_success) {
|
||||
printf("read all data failed\n");
|
||||
PrivFree(data_frame_ptr);
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(data_frame_ptr->id, FRAME_ID, strlen(FRAME_ID));
|
||||
printf("data_frame_ptr->id: %s\n", data_frame_ptr->id);
|
||||
|
||||
char *json_str = cJSON_Print(root);
|
||||
strncpy((char *)data_frame_ptr->data, json_str, MAX_DATA_SIZE - 1);
|
||||
data_frame_ptr->data[MAX_DATA_SIZE - 1] = '\0'; // 确保结尾是 \0
|
||||
printf("data_frame_ptr->data: %s\n", data_frame_ptr->data);
|
||||
|
||||
// 删除字符串空间
|
||||
free(json_str);
|
||||
// 删除 cJSON 对象
|
||||
cJSON_Delete(root);
|
||||
|
||||
/* 将解析后的数据帧放入循环队列 */
|
||||
PrivMutexObtain(&queue_buffer_ptr->mutex); // 获取互斥锁
|
||||
OfferBuffer(queue_buffer_ptr, data_frame_ptr); // 将数据帧放入队列
|
||||
printf("receive data from DTSD342, id: %s\n", data_frame_ptr->id); // 打印接收到的数据帧ID
|
||||
PrivMutexAbandon(&queue_buffer_ptr->mutex); // 释放互斥锁
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量,即告知发送数据线程,队列中有新的数据帧
|
||||
|
||||
PrivTaskDelay(RECEIVE_DATA_INTERVAL_MS); // 延迟一段时间再读取下一帧数据
|
||||
|
||||
printf("end cycle\n");
|
||||
}
|
||||
|
||||
PrivClose(fd); // 关闭设备文件
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过4G向服务器发送数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *SendDataToServerTask_4G(void *arg) {
|
||||
uint8_t server_ip_address[16] = {}; // 目的IP地址
|
||||
uint8_t server_port[6] = {}; // 目的端口号
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
unsigned char receive_buffer[256]; // 从服务器接收每帧响应的存储空间
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); // 查找4G模块适配器
|
||||
|
||||
AdapterDeviceOpen(adapter); // 打开适配器对应的设备(实际打开串口中断)
|
||||
int baud_rate = BAUD_RATE_115200; // 波特率,用于设置4G模块串口
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate); // 对适配器对应设备进行配置(实际配置波特率)
|
||||
|
||||
struct DataFrame *data_frame_ptr = NULL; // 数据帧定义
|
||||
while (1) {
|
||||
PrivSemaphoreObtainWait(&queue_buffer_ptr->full, NULL); // 尝试获取循环队列队头元素,如果获取信号量失败,则等待信号量
|
||||
#ifdef BSP_BLE_CONFIG // 如果启用了BLE配置功能
|
||||
/* 获取互斥锁 */
|
||||
PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待
|
||||
|
||||
/* 尝试连接服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_4G[1],
|
||||
CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_4G*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
if (CFG->mqttSwitch_4G == 1) { // 如果使能MQTT
|
||||
AdapterDeviceMqttConnect(adapter, server_ip_address, server_port, CFG->mqttClientId_4G, CFG->mqttUsername_4G,
|
||||
CFG->mqttPassword_4G);
|
||||
} else { // 如果禁用MQTT
|
||||
AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4);
|
||||
}
|
||||
|
||||
AdapterDeviceNetstat(adapter); // 读取网络连接状态
|
||||
/* 若连接失败,则等待10s再次尝试连接 */
|
||||
if (CFG->mqttSwitch_4G == 0 && !adapter->network_info.is_connected ||
|
||||
CFG->mqttSwitch_4G == 1 && !adapter->network_info.mqttIsConnected) {
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
printf("4G connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#else // 如果没有启用BLE配置功能
|
||||
/* 尝试连接到服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_4G[1],
|
||||
CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_4G*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int reconnect_count = RECONNECT_COUNT; // 尝试重新连接服务器最多RECONNECT_COUNT次
|
||||
while (reconnect_count > 0) {
|
||||
int res;
|
||||
if (CFG->mqttSwitch_4G == 1) {
|
||||
res = AdapterDeviceMqttConnect(adapter, mqttServerIp, mqttServerPort, CFG->mqttClientId_4G, CFG->mqttUsername_4G,
|
||||
CFG->mqttPassword_4G);
|
||||
} else {
|
||||
res = AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4);
|
||||
}
|
||||
if (res == 0) {
|
||||
break;
|
||||
}
|
||||
reconnect_count--;
|
||||
}
|
||||
if (reconnect_count <= 0) { // 若RECONNECT_COUNT次都连接失败,则等待10s再次尝试连接
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
printf("4G connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
PrivMutexObtain(&queue_buffer_ptr->mutex); // 获取互斥锁
|
||||
data_frame_ptr = PollBuffer(queue_buffer_ptr); // 从队列中获取数据帧
|
||||
PrivMutexAbandon(&queue_buffer_ptr->mutex); // 释放互斥锁
|
||||
|
||||
int resend_count = RESEND_COUNT; // 定义数据帧重发次数
|
||||
while (data_frame_ptr != NULL && resend_count > 0) { // 只有数据帧非空并且还有剩余重发次数,才进行发送
|
||||
/* 向服务器发送数据 */
|
||||
printf("data_frame_ptr->data: %s", data_frame_ptr->data);
|
||||
printf("send data to server, id: %s\n", data_frame_ptr->id);
|
||||
if (CFG->mqttSwitch_4G == 1) { // MQTT模式下,无需服务器响应数据
|
||||
AdapterDeviceMqttSend(adapter, CFG->mqttTopic_4G, data_frame_ptr->data,
|
||||
strlen(data_frame_ptr->data)); // 发送数据,注意当前最多发送256字节
|
||||
break;
|
||||
} else {
|
||||
AdapterDeviceSend(adapter, data_frame_ptr->data,
|
||||
strlen(data_frame_ptr->data)); // 发送数据,注意当前最多发送256字节
|
||||
|
||||
/* 从服务器接收响应,约定服务器接收完数据帧后,返回数据帧中的前12个字节,即数据帧id */
|
||||
/* 多读取2字节,是为了防止前面还有命令模式返回的剩余的\r\n影响判断 */
|
||||
memset(receive_buffer, 0, sizeof(receive_buffer));
|
||||
int receive_length = AdapterDeviceRecv(adapter, receive_buffer, strlen(data_frame_ptr->id) + 2);
|
||||
if (receive_length == strlen(data_frame_ptr->id) + 2 || receive_length == strlen(data_frame_ptr->id)) {
|
||||
/* 打印服务器响应 */
|
||||
printf("receive_length: %d\n", receive_length);
|
||||
printf("receive_buffer: ");
|
||||
for (int i = 0; i < receive_length; i++) {
|
||||
printf("%c", receive_buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
/* 比较服务器响应的内容与发送的数据帧id是否一致 */
|
||||
if (strstr(receive_buffer, data_frame_ptr->id) != NULL) {
|
||||
break; // 接收成功,退出循环
|
||||
}
|
||||
} else {
|
||||
printf("receive_length: %d\n", receive_length);
|
||||
printf("receive_buffer: ");
|
||||
for (int i = 0; i < receive_length; i++) {
|
||||
printf("%d ", receive_buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
resend_count--;
|
||||
}
|
||||
if (data_frame_ptr != NULL) {
|
||||
PrivFree(data_frame_ptr); // 释放数据帧内存
|
||||
data_frame_ptr = NULL; // 避免野指针
|
||||
}
|
||||
// AdapterDeviceDisconnect(adapter, NULL); // 关闭适配器对应的设备
|
||||
#ifdef BSP_BLE_CONFIG
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
#endif
|
||||
if (resend_count <= 0) { // 如果数据帧重发次数超过上限,表示发送失败,丢弃该帧
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过以太网向服务器发送数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *SendDataToServerTask_Ethernet(void *arg) {
|
||||
uint8_t server_ip_address[16] = {}; // 目的IP地址
|
||||
uint8_t server_port[6] = {}; // 目的端口号
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
unsigned char receive_buffer[256]; // 从服务器接收每帧响应的存储空间
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME); // 查找以太网模块适配器
|
||||
|
||||
#ifndef BSP_BLE_CONFIG // 如果没有使能蓝牙配置功能
|
||||
AdapterDeviceSetUp(adapter); // 启动以太网主任务线程
|
||||
AdapterDeviceSetDhcp(adapter, CFG->dhcpSwitch_Ethernet); // 启用或禁用DHCP
|
||||
#endif
|
||||
|
||||
struct DataFrame *data_frame_ptr = NULL; // 数据帧定义
|
||||
while (1) {
|
||||
PrivSemaphoreObtainWait(&queue_buffer_ptr->full, NULL); // 尝试获取循环队列队头元素,如果获取信号量失败,则等待信号量
|
||||
#ifdef BSP_BLE_CONFIG // 使能蓝牙配置功能
|
||||
/* 获取互斥锁 */
|
||||
PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待;
|
||||
|
||||
/* 尝试连接服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1],
|
||||
CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_Ethernet*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int res = AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4);
|
||||
|
||||
/* 连接失败,则等待10s再次尝试连接 */
|
||||
if (res != 0 && res != 0x1D) {
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
printf("Ethernet connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
/* 尝试连接到服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1],
|
||||
CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_Ethernet*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int reconnect_count = RECONNECT_COUNT; // 尝试重新连接服务器最多RECONNECT_COUNT次
|
||||
while (reconnect_count > 0) {
|
||||
int res = AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4); // 尝试连接服务器
|
||||
if (res == 0 || res == 0x1D) {
|
||||
break;
|
||||
}
|
||||
reconnect_count--;
|
||||
}
|
||||
if (reconnect_count <= 0) { // 若RECONNECT_COUNT次都连接失败,则等待10s再次尝试连接
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
printf("Ethernet connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
PrivMutexObtain(&queue_buffer_ptr->mutex); // 获取互斥锁
|
||||
data_frame_ptr = PollBuffer(queue_buffer_ptr); // 从队列中获取数据帧
|
||||
PrivMutexAbandon(&queue_buffer_ptr->mutex); // 释放互斥锁
|
||||
|
||||
int resend_count = RESEND_COUNT; // 定义数据帧重发次数
|
||||
|
||||
/* 只有数据帧非空并且还有剩余重发次数,才进行发送 */
|
||||
while (data_frame_ptr != NULL && resend_count > 0) {
|
||||
/* 向服务器发送数据 */
|
||||
printf("send data to server, id: %s\n", data_frame_ptr->id);
|
||||
printf("data_frame_ptr->data: %s", data_frame_ptr->data);
|
||||
AdapterDeviceSend(adapter, data_frame_ptr->data,
|
||||
strlen(data_frame_ptr->data)); // 发送数据,注意当前最多发送256字节
|
||||
|
||||
/* 从服务器接收响应,约定服务器接收完数据帧后,返回数据帧中的前12个字节,即数据帧id */
|
||||
memset(receive_buffer, 0, sizeof(receive_buffer));
|
||||
PrivTaskDelay(6000);
|
||||
if (AdapterDeviceRecv(adapter, receive_buffer, strlen(data_frame_ptr->id)) == strlen(data_frame_ptr->id)) {
|
||||
/* 打印服务器响应 */
|
||||
printf("receive_buffer: ");
|
||||
for (int i = 0; i < strlen(receive_buffer); i++) {
|
||||
printf("%c", receive_buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
/* 比较服务器响应的内容与发送的数据帧id是否一致 */
|
||||
if (strstr(data_frame_ptr->id, receive_buffer) != NULL) {
|
||||
break; // 接收成功,退出循环
|
||||
}
|
||||
}
|
||||
resend_count--;
|
||||
}
|
||||
if (data_frame_ptr != NULL) {
|
||||
PrivFree(data_frame_ptr); // 释放数据帧内存
|
||||
data_frame_ptr = NULL; // 避免野指针
|
||||
}
|
||||
AdapterDeviceDisconnect(adapter, NULL);
|
||||
#ifdef BSP_BLE_CONFIG
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
#endif
|
||||
if (resend_count <= 0) { // 如果数据帧重发次数超过上限,表示发送失败,丢弃该帧
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 开启从DTSD342接收数据的线程以及上传数据到服务器的线程,此方法在main方法中被调用,开机或复位启动
|
||||
*/
|
||||
void StartUpTransformDataTask(void) {
|
||||
/* 分配循环队列空间 */
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)PrivCalloc(1, sizeof(struct QueueBuffer));
|
||||
if (InitBuffer(queue_buffer_ptr) < 0) {
|
||||
PrivFree(queue_buffer_ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 启动从DTSD342接收数据的线程 */
|
||||
pthread_attr_t receive_data_from_dtsd342_task_attr;
|
||||
pthread_args_t receive_data_from_dtsd342_task_args;
|
||||
receive_data_from_dtsd342_task_attr.schedparam.sched_priority = 16; // 线程优先级
|
||||
receive_data_from_dtsd342_task_attr.stacksize = 2048; // 线程栈大小
|
||||
receive_data_from_dtsd342_task_args.pthread_name = "ReceiveDataFromDTSD342Task"; // 线程名字
|
||||
receive_data_from_dtsd342_task_args.arg = queue_buffer_ptr; // 线程参数
|
||||
pthread_t receive_data_thread; // 线程ID
|
||||
PrivTaskCreate(&receive_data_thread, &receive_data_from_dtsd342_task_attr, ReceiveDataFromDTSD342Task, &receive_data_from_dtsd342_task_args);
|
||||
PrivTaskStartup(&receive_data_thread);
|
||||
|
||||
/* 启动上传数据到服务器的线程 */
|
||||
pthread_attr_t send_data_to_server_task_attr;
|
||||
pthread_args_t send_data_to_server_task_args;
|
||||
send_data_to_server_task_attr.schedparam.sched_priority = 16; // 线程优先级
|
||||
send_data_to_server_task_attr.stacksize = 2200; // 线程栈大小
|
||||
send_data_to_server_task_args.pthread_name = "SendDataToServerTask"; // 线程名字
|
||||
send_data_to_server_task_args.arg = queue_buffer_ptr; // 线程参数
|
||||
pthread_t send_data_thread; // 线程ID
|
||||
void *(*start_routine)(void *) = SendDataToServerTask_4G; // 通过4G模块上传到服务器
|
||||
// void *(*start_routine)(void *) = SendDataToServerTask_Ethernet; // 通过以太网模块上传到服务器
|
||||
PrivTaskCreate(&send_data_thread, &send_data_to_server_task_attr, start_routine, &send_data_to_server_task_args); // 通过4G模块上传到服务器
|
||||
PrivTaskStartup(&send_data_thread);
|
||||
}
|
||||
|
|
@ -1,862 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file .c
|
||||
* @brief Support reading data from WISDOM DTZ178 using the DL/T645-2007 protocol
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2025.4.14
|
||||
*/
|
||||
|
||||
#include <ModuleConfig.h>
|
||||
#include <adapter.h>
|
||||
#include <transform.h>
|
||||
#include <math.h>
|
||||
#include <cJSON.h>
|
||||
|
||||
#define MAX_FRAME_SIZE 256 // 最大帧大小
|
||||
#define MAX_DATA_SIZE 1024 // 最大数据大小
|
||||
#define MAX_BUFFER_SIZE 1024 * 2 // 最大缓冲区大小
|
||||
#define RECEIVE_DATA_INTERVAL_MS 1000 * 60 * 2 // DTZ178数据采集间隔时间,单位为毫秒
|
||||
#define SAMPLE_DATA_INTERVAL_MS 300 // 稳定连续采集的间隔时间,单位为毫秒
|
||||
#define SEND_FRAME_LEN 16 // 发送帧长度
|
||||
#define BASE_RECEIVE_FRAME_LEN 16 // 基础返回帧长度(不包含各项数据长度)
|
||||
#define TOTAL_RECEIVE_FRAME_LEN 20 // 总返回帧长度(不包含各项数据长度),包含4个前置0xFE字节
|
||||
#define RESEND_COUNT 3 // 最大帧重发次数
|
||||
#define RECONNECT_COUNT 5 // 最大连接次数
|
||||
#define WATING_RESPONSE_MS 5000 // 等待响应时间,单位为毫秒
|
||||
#define DATA_COUNT (sizeof(data_id_map) / sizeof(data_id_map[0])) // 数据项数量
|
||||
#define READ_COMMAND 0x11 // 读取数据指令
|
||||
|
||||
static const uint8_t addr_meter[6] = {0x01, 0x22, 0x00, 0x03, 0x61, 0x74};
|
||||
static uint8_t parsed_date[11] = {0};
|
||||
static uint8_t parsed_time[9] = {0};
|
||||
|
||||
#ifndef DATA_ITEMS_DEF_H
|
||||
#define DATA_ITEMS_DEF_H
|
||||
|
||||
#define DATA_ITEMS_XMACRO \
|
||||
X(ENERGY_TOTAL, 0x00, 0x00, 0x00, 0x00, 4, 2) /* 总电能,4字节,2位小数,单位kWh */ \
|
||||
X(ENERGY_ACTIVE, 0x00, 0x01, 0x00, 0x00, 4, 2) /* 有功电能,4字节,2位小数,单位kWh */ \
|
||||
X(ENERGY_REACTIVE, 0x00, 0x02, 0x00, 0x00, 4, 2) /* 无功电能,4字节,2位小数,单位kWh */ \
|
||||
X(VOLTAGE_A, 0x02, 0x01, 0x01, 0x00, 2, 1) /* A相电压,2字节,1位小数,单位V */ \
|
||||
X(VOLTAGE_B, 0x02, 0x01, 0x02, 0x00, 2, 1) /* B相电压,2字节,1位小数,单位V */ \
|
||||
X(VOLTAGE_C, 0x02, 0x01, 0x03, 0x00, 2, 1) /* C相电压,2字节,1位小数,单位V */ \
|
||||
X(CURRENT_A, 0x02, 0x02, 0x01, 0x00, 3, 3) /* A相电流,3字节,3位小数,单位A */ \
|
||||
X(CURRENT_B, 0x02, 0x02, 0x02, 0x00, 3, 3) /* B相电流,3字节,3位小数,单位A */ \
|
||||
X(CURRENT_C, 0x02, 0x02, 0x03, 0x00, 3, 3) /* C相电流,3字节,3位小数,单位A */ \
|
||||
X(ACTIVE_POWER_TOTAL, 0x02, 0x03, 0x00, 0x00, 3, 4) /* 总有功功率,3字节,4位小数,单位kW */ \
|
||||
X(ACTIVE_POWER_A, 0x02, 0x03, 0x01, 0x00, 3, 4) /* A有功功率,3字节,4位小数,单位kW */ \
|
||||
X(ACTIVE_POWER_B, 0x02, 0x03, 0x02, 0x00, 3, 4) /* B有功功率,3字节,4位小数,单位kW */ \
|
||||
X(ACTIVE_POWER_C, 0x02, 0x03, 0x03, 0x00, 3, 4) /* C有功功率,3字节,4位小数,单位kW */ \
|
||||
X(REACTIVE_POWER_TOTAL, 0x02, 0x04, 0x00, 0x00, 3, 4) /* 总无功功率,3字节,4位小数,单位kvar */ \
|
||||
X(REACTIVE_POWER_A, 0x02, 0x04, 0x01, 0x00, 3, 4) /* A无功功率,3字节,4位小数,单位kvar */ \
|
||||
X(REACTIVE_POWER_B, 0x02, 0x04, 0x02, 0x00, 3, 4) /* B无功功率,3字节,4位小数,单位kvar */ \
|
||||
X(REACTIVE_POWER_C, 0x02, 0x04, 0x03, 0x00, 3, 4) /* C无功功率,3字节,4位小数,单位kvar */ \
|
||||
X(APPARENT_POWER_TOTAL, 0x02, 0x05, 0x00, 0x00, 3, 4) /* 总视在功率,3字节,4位小数,单位kVA */ \
|
||||
X(APPARENT_POWER_A, 0x02, 0x05, 0x01, 0x00, 3, 4) /* A视在功率,3字节,4位小数,单位kVA */ \
|
||||
X(APPARENT_POWER_B, 0x02, 0x05, 0x02, 0x00, 3, 4) /* B视在功率,3字节,4位小数,单位kVA */ \
|
||||
X(APPARENT_POWER_C, 0x02, 0x05, 0x03, 0x00, 3, 4) /* C视在功率,3字节,4位小数,单位kVA */ \
|
||||
X(POWER_FACTOR_TOTAL, 0x02, 0x06, 0x00, 0x00, 2, 3) /* 总功率因数,2字节,3位小数,单位无 */ \
|
||||
X(POWER_FACTOR_A, 0x02, 0x06, 0x01, 0x00, 2, 3) /* A功率因数,2字节,3位小数,单位无 */ \
|
||||
X(POWER_FACTOR_B, 0x02, 0x06, 0x02, 0x00, 2, 3) /* B功率因数,2字节,3位小数,单位无 */ \
|
||||
X(POWER_FACTOR_C, 0x02, 0x06, 0x03, 0x00, 2, 3) /* C功率因数,2字节,3位小数,单位无 */ \
|
||||
X(FREQUENCY, 0x02, 0x80, 0x00, 0x02, 2, 2) /* 电网频率,2字节,2位小数,单位Hz */ \
|
||||
X(INTERNAL_TEMPERATURE, 0x02, 0x80, 0x00, 0x07, 2, 1) /* 内部温度,2字节,1位小数,单位℃ */ \
|
||||
X(DATE_YMD, 0x04, 0x00, 0x01, 0x01, 4, 0) /* 年月日星期,4字节,0位小数,单位无*/ \
|
||||
X(TIME_HMS, 0x04, 0x00, 0x01, 0x02, 3, 0) /* 时分秒,3字节,0位小数,单位无 */
|
||||
|
||||
|
||||
#endif // DATA_ITEMS_DEF_H
|
||||
|
||||
#ifndef DATA_ITEMS_H
|
||||
#define DATA_ITEMS_H
|
||||
|
||||
typedef enum {
|
||||
#define X(name, id3, id2, id1, id0, size, dec) name,
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
} DataIdIndex;
|
||||
|
||||
typedef struct {
|
||||
uint8_t byte_size;
|
||||
uint8_t decimal_places;
|
||||
} DataInfo;
|
||||
|
||||
static const uint8_t data_id_map[][4] = {
|
||||
#define X(name, id3, id2, id1, id0, size, dec) {id3, id2, id1, id0},
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
};
|
||||
|
||||
static const DataInfo data_info_map[] = {
|
||||
#define X(name, id3, id2, id1, id0, size, dec) {size, dec},
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
};
|
||||
|
||||
static const char *data_id_names[] = {
|
||||
#define X(name, id3, id2, id1, id0, size, dec) #name,
|
||||
DATA_ITEMS_XMACRO
|
||||
#undef X
|
||||
};
|
||||
|
||||
#endif // DATA_ITEMS_H
|
||||
|
||||
/**
|
||||
* @brief 计算DL/T645-2007数据帧的校验和
|
||||
* @param data 数据帧指针
|
||||
* @param len 校验的长度(从第一个 0x68 开始,到CS前一位)
|
||||
* @return uint8_t 校验和
|
||||
*/
|
||||
static uint8_t CheckSum(const uint8_t *data, uint8_t len)
|
||||
{
|
||||
uint16_t sum = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
sum += data[i];
|
||||
}
|
||||
return (uint8_t)(sum & 0xFF); // 返回低8位
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 构造DL/T645-2007协议数据帧
|
||||
* @param addr 6字节地址(低位在前)
|
||||
* @param ctrl_code 控制码,如0x11表示读数据
|
||||
* @param data_id 4字节数据标识(低位在前)
|
||||
* @param data 数据域内容(可为空)
|
||||
* @param data_len 数据域长度(读数据时<=200,写数据时<=50)
|
||||
* @param request_frame 输出构造好的完整帧
|
||||
* @return int 0成功,-1失败
|
||||
*/
|
||||
static int GenerateRequestFrame(const uint8_t addr[6], uint8_t ctrl_code, const uint8_t data_id[4], const uint8_t *data, uint8_t data_len, uint8_t *request_frame)
|
||||
{
|
||||
if (!addr || !data_id || !request_frame)
|
||||
return -1;
|
||||
|
||||
uint8_t idx = 0;
|
||||
request_frame[idx++] = 0x68;
|
||||
|
||||
// 地址 6 字节(低位在前)
|
||||
for (int i = 5; i >= 0; i--)
|
||||
request_frame[idx++] = addr[i];
|
||||
|
||||
request_frame[idx++] = 0x68;
|
||||
request_frame[idx++] = ctrl_code;
|
||||
|
||||
uint8_t total_data_len = 4 + data_len;
|
||||
request_frame[idx++] = total_data_len;
|
||||
|
||||
// 数据标识 + 数据内容,低位在前,+0x33加密
|
||||
for (int i = 3; i >= 0; i--)
|
||||
request_frame[idx++] = data_id[i] + 0x33;
|
||||
|
||||
for (int i = 0; i < data_len; i++)
|
||||
request_frame[idx++] = data[i] + 0x33;
|
||||
|
||||
// 校验和
|
||||
uint8_t cs = CheckSum(request_frame, idx);
|
||||
request_frame[idx++] = cs;
|
||||
|
||||
// 结束码
|
||||
request_frame[idx++] = 0x16;
|
||||
|
||||
// printf("GenerateRequestFrame: ");
|
||||
// for (int i = 0; i < idx; i++) {
|
||||
// printf("%02X ", request_frame[i]);
|
||||
// }
|
||||
// printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将要上传服务器的数据帧
|
||||
*/
|
||||
struct DataFrame {
|
||||
unsigned char id[20]; // 用响应的时间戳作为数据帧的id
|
||||
unsigned char data[MAX_DATA_SIZE]; // 上传服务器的数据帧字符串,用JSON封装
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Modbus RTU响应数据帧的缓存,使用循环队列作为数据结构
|
||||
*/
|
||||
struct QueueBuffer {
|
||||
struct DataFrame *buffer[MAX_BUFFER_SIZE / sizeof(struct DataFrame)]; // 循环队列存储空间,使用数组存储
|
||||
int front; // 循环队列队头
|
||||
int rear; // 循环队列队尾
|
||||
pthread_mutex_t mutex; // 互斥访问循环队列信号量
|
||||
sem_t full; // 循环队列中有效成员个数的信号量
|
||||
};
|
||||
|
||||
#define BUFFER_ELEM_COUNT (MAX_BUFFER_SIZE / sizeof(struct DataFrame)) // 循环队列中可以容纳的最大成员个数
|
||||
|
||||
/**
|
||||
* @brief 初始化循环队列
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @return * int 0表示初始化成功,其他表示初始化失败
|
||||
*/
|
||||
static int InitBuffer(struct QueueBuffer *queue_buffer_ptr) {
|
||||
queue_buffer_ptr->front = 0;
|
||||
queue_buffer_ptr->rear = 0;
|
||||
if (PrivMutexCreate(&queue_buffer_ptr->mutex, 0) < 0) {
|
||||
printf("buffer mutex create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
if (PrivSemaphoreCreate(&queue_buffer_ptr->full, 0, 0) < 0) {
|
||||
printf("buffer full semaphore create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 循环队列入队,如果循环队列已满,则将最旧的成员出队后,新成员再入队
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @param data_frame_ptr DTZ178响应数据帧
|
||||
* @return int 0表示入队成功,其他表示入队失败
|
||||
*/
|
||||
static int OfferBuffer(struct QueueBuffer *queue_buffer_ptr, struct DataFrame *data_frame_ptr) {
|
||||
/* 循环队列已满,将最旧的成员出队 */
|
||||
if ((queue_buffer_ptr->rear + 1) % BUFFER_ELEM_COUNT == queue_buffer_ptr->front) {
|
||||
struct DataFrame *front_data_frame_ptr = queue_buffer_ptr->buffer[queue_buffer_ptr->front];
|
||||
PrivFree(front_data_frame_ptr);
|
||||
queue_buffer_ptr->front = (queue_buffer_ptr->front + 1) % BUFFER_ELEM_COUNT;
|
||||
}
|
||||
/* 新成员入队 */
|
||||
queue_buffer_ptr->buffer[queue_buffer_ptr->rear] = data_frame_ptr;
|
||||
queue_buffer_ptr->rear = (queue_buffer_ptr->rear + 1) % BUFFER_ELEM_COUNT;
|
||||
printf("front: %d\n", queue_buffer_ptr->front);
|
||||
printf("rear: %d\n", queue_buffer_ptr->rear);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 循环队列出队,如果队列为空则返回NULL
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @return struct DataFrame* 出队成员,如果队列为空则返回NULL
|
||||
*/
|
||||
static struct DataFrame *PollBuffer(struct QueueBuffer *queue_buffer_ptr) {
|
||||
/* 队列为空,返回NULL */
|
||||
if (queue_buffer_ptr->front == queue_buffer_ptr->rear) {
|
||||
return NULL;
|
||||
}
|
||||
/* 最旧的成员出队 */
|
||||
struct DataFrame *front_data_frame_ptr = queue_buffer_ptr->buffer[queue_buffer_ptr->front];
|
||||
queue_buffer_ptr->buffer[queue_buffer_ptr->front] = NULL;
|
||||
queue_buffer_ptr->front = (queue_buffer_ptr->front + 1) % BUFFER_ELEM_COUNT;
|
||||
printf("front: %d\n", queue_buffer_ptr->front);
|
||||
printf("rear: %d\n", queue_buffer_ptr->rear);
|
||||
return front_data_frame_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 查看队头元素,如果队列为空则返回NULL
|
||||
* @param queue_buffer_ptr 循环队列指针
|
||||
* @return struct DataFrame* 队头元素,如果队列为空则返回NULL
|
||||
*/
|
||||
static struct DataFrame *PeekBuffer(struct QueueBuffer *queue_buffer_ptr) {
|
||||
/* 如果队列为空,返回NULL */
|
||||
if (queue_buffer_ptr->front == queue_buffer_ptr->rear) {
|
||||
return NULL;
|
||||
}
|
||||
/* 返回队头元素,但不出队 */
|
||||
return queue_buffer_ptr->buffer[queue_buffer_ptr->front];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 改写PrivRead函数,原有函数只会读取接收缓冲区的当前已有字节,新函数会读取指定字节数再返回
|
||||
* @param fd 文件描述符
|
||||
* @param buf 数据读取到的位置
|
||||
* @param len 读取的指定字节数
|
||||
* @return int 如果读取到指定字节数返回0;如果到达WATING_RESPONSE_MS仍未读取到指定字节数,或者读数据错误,返回-1
|
||||
*/
|
||||
static int PrivReadEnoughData(int fd, void *buf, size_t len) {
|
||||
char *buffer = (char *)buf; // 将接收的存储空间指针强制转型
|
||||
int gotten_bytes = 0; // 已经读取到的字节数
|
||||
int remain_time = WATING_RESPONSE_MS; // 剩余的时间
|
||||
|
||||
/* 只有接收的字节数不够,并且还有剩余时间,才可以继续读取 */
|
||||
while (gotten_bytes < len && remain_time > 0) {
|
||||
int bytes = PrivRead(fd, buffer + gotten_bytes, len - gotten_bytes); // 从设备读取
|
||||
gotten_bytes += bytes;
|
||||
PrivTaskDelay(100); // 每100ms读取一次
|
||||
remain_time -= 100; // 剩余时间减去100ms
|
||||
}
|
||||
/* 若没有剩余时间,表示还没有读取到指定的字节数,返回-1;若有剩余时间,表示已经读取了指定的字节数,返回0 */
|
||||
return remain_time < 0 ? -1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将BCD格式的多字节数值转换为十进制整数
|
||||
* @param bcd BCD格式的原始数值(低字节在低位)
|
||||
* @param bytes BCD所占用的字节数
|
||||
* @return 转换后的十进制整数结果
|
||||
* @note 每个字节的高4位和低4位分别代表1位十进制数字,即每个字节表示两位十进制数。
|
||||
* 最低位字节代表最低的两位十进制数,依此类推。例如 0x12 0x34 表示1234。
|
||||
*/
|
||||
static uint32_t BcdToDecimal(uint32_t bcd, uint8_t bytes)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
for (int i = 0; i < bytes; i++) {
|
||||
uint8_t byte = (bcd >> (i * 8)) & 0xFF;
|
||||
uint8_t high = (byte >> 4) & 0x0F; // 高4位
|
||||
uint8_t low = byte & 0x0F; // 低4位
|
||||
result += (high * 10 + low) * pow(100, i); // 每个字节是2位十进制
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析DTZ178响应的Modbus RTU数据帧
|
||||
* @param response_frame 返回的数据帧
|
||||
* @param data_info_ptr 数据项的字节数和小数位数信息
|
||||
* @param item_id 数据项ID
|
||||
* @param root 根节点
|
||||
*/
|
||||
static void ParseResponseFrame(unsigned char *response_frame, const DataInfo *data_info_ptr, int item_id, cJSON *root) {
|
||||
if (!response_frame || !data_info_ptr || !root) {
|
||||
printf("Invalid input to ParseResponseFrame\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t data_len_total = response_frame[13]; // 数据域长度,包括数据标识(4字节) + 数据内容
|
||||
if (data_len_total < 4 || data_len_total - 4 != data_info_ptr->byte_size) {
|
||||
printf("Invalid data length in frame\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *data_field = &response_frame[14]; // 数据域开始位置(含数据标识 + 数据内容)
|
||||
const uint8_t *data_value = &data_field[4]; // 数据内容起始位置
|
||||
|
||||
// for (int i = 0; i < data_info_ptr->byte_size; i++)
|
||||
// printf("%02x ", data_value[i]);
|
||||
|
||||
// 判断是否是日期或时间
|
||||
if (item_id == DATE_YMD) {
|
||||
snprintf(parsed_date, sizeof(parsed_date), "20%02x-%02x-%02x", data_value[3] - 0x33, data_value[2] - 0x33, data_value[1] - 0x33);
|
||||
// printf("Parsed date: %s\n", parsed_date);
|
||||
return;
|
||||
} else if (item_id == TIME_HMS) {
|
||||
snprintf(parsed_time, sizeof(parsed_time), "%02x:%02x:%02x", data_value[2] - 0x33, data_value[1] - 0x33, data_value[0] - 0x33);
|
||||
// printf("Parsed time: %s\n", parsed_time);
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t bcd = 0;
|
||||
|
||||
for (int i = data_info_ptr->byte_size - 1; i >= 0; i--) {
|
||||
// 解密(减去0x33),反向拼接(低位在前 → 高位在前)
|
||||
bcd |= ((uint32_t)(data_value[i] - 0x33)) << (8 * i);
|
||||
}
|
||||
|
||||
uint32_t value = 0;
|
||||
value = BcdToDecimal(bcd, data_info_ptr->byte_size);
|
||||
|
||||
// 除以10的倍数来处理小数点
|
||||
double final_value = value / pow(10, data_info_ptr->decimal_places);
|
||||
|
||||
// printf("Parsed value: %.*f\n", data_info_ptr->decimal_places, final_value);
|
||||
|
||||
char format[10];
|
||||
snprintf(format, sizeof(format), "%%.%df", data_info_ptr->decimal_places);
|
||||
|
||||
char final_value_str[20];
|
||||
snprintf(final_value_str, sizeof(final_value_str), format, final_value);
|
||||
|
||||
cJSON_AddStringToObject(root, data_id_names[item_id], final_value_str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 从DTZ178接收数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *ReceiveDataFromDTZ178Task(void *arg) {
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
int fd = PrivOpen("/dev/rs485_dev1", O_RDWR); // 打开设备文件
|
||||
if (fd < 0) { // 打开设备文件失败,打印错误信息
|
||||
printf("open rs485 fd error: %d\n", fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct SerialDataCfg rs485_configuration;
|
||||
memset(&rs485_configuration, 0, sizeof(struct SerialDataCfg));
|
||||
/* 读取RS485配置信息 */
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待
|
||||
int baud_rates_option = CFG->baudRate_Rs485;
|
||||
int data_bits_option = CFG->dataBits_Rs485;
|
||||
int stop_bits_option = CFG->stopBits_Rs485;
|
||||
int parity_option = CFG->parity_Rs485;
|
||||
PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁
|
||||
switch (baud_rates_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_2400; // 默认波特率为2400
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_4800;
|
||||
break;
|
||||
case 3:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_9600;
|
||||
break;
|
||||
case 4:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_19200;
|
||||
break;
|
||||
case 5:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_38400;
|
||||
break;
|
||||
case 6:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_57600;
|
||||
break;
|
||||
case 7:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_115200;
|
||||
break;
|
||||
case 8:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_230400;
|
||||
break;
|
||||
default:
|
||||
rs485_configuration.serial_baud_rate = BAUD_RATE_9600;
|
||||
break;
|
||||
}
|
||||
switch (data_bits_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_data_bits = DATA_BITS_8;
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_data_bits = DATA_BITS_9;
|
||||
break;
|
||||
default:
|
||||
rs485_configuration.serial_data_bits = DATA_BITS_8;
|
||||
break;
|
||||
}
|
||||
switch (stop_bits_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_stop_bits = STOP_BITS_1;
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_stop_bits = STOP_BITS_2;
|
||||
break;
|
||||
default:
|
||||
rs485_configuration.serial_stop_bits = STOP_BITS_1;
|
||||
break;
|
||||
}
|
||||
switch (parity_option) {
|
||||
case 1:
|
||||
rs485_configuration.serial_parity_mode = PARITY_NONE;
|
||||
break;
|
||||
case 2:
|
||||
rs485_configuration.serial_parity_mode = PARITY_ODD;
|
||||
break;
|
||||
case 3:
|
||||
rs485_configuration.serial_parity_mode = PARITY_EVEN; // 默认校验方式为偶校验
|
||||
break;
|
||||
}
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = (void *)&rs485_configuration;
|
||||
if (0 != PrivIoctl(fd, OPE_INT, &ioctl_cfg)) {
|
||||
printf("ioctl uart fd error %d\n", fd);
|
||||
PrivClose(fd);
|
||||
return NULL;
|
||||
}
|
||||
printf("open rs485 fd success %d\n", fd);
|
||||
|
||||
unsigned char request_frame[MAX_FRAME_SIZE]; // 定义请求帧
|
||||
unsigned char response_frame[MAX_FRAME_SIZE]; // 定义回复帧
|
||||
while (1) {
|
||||
printf("enter cycle\n");
|
||||
struct DataFrame *data_frame_ptr = (struct DataFrame *)PrivMalloc(sizeof(struct DataFrame));
|
||||
memset(data_frame_ptr, 0, sizeof(struct DataFrame));
|
||||
|
||||
// 创建一个空的JSON对象
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
int is_success = 1;
|
||||
|
||||
for (int i = 0; i < DATA_COUNT; i++) {
|
||||
const uint8_t *data_id = data_id_map[i];
|
||||
const DataInfo *data_info = &data_info_map[i];
|
||||
|
||||
memset(request_frame, 0, sizeof(request_frame));
|
||||
if (GenerateRequestFrame(addr_meter, READ_COMMAND, data_id, NULL, 0, request_frame) < 0) {
|
||||
printf("Generate frame failed for index %d\n", i);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
PrivWrite(fd, request_frame, SEND_FRAME_LEN); // 发送Modbus RTU请求帧
|
||||
|
||||
/* 读取响应帧数据 */
|
||||
memset(response_frame, 0, sizeof(response_frame));
|
||||
if (PrivReadEnoughData(fd, response_frame, TOTAL_RECEIVE_FRAME_LEN + data_info->byte_size) < 0) {
|
||||
printf("Timeout reading response for index %d\n", i);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// printf("Response frame for index %d: ", i);
|
||||
// for (int j = 0; j < TOTAL_RECEIVE_FRAME_LEN + data_info->byte_size; j++)
|
||||
// printf("%02X ", response_frame[j]);
|
||||
// printf("\n");
|
||||
|
||||
// 校验帧头、帧尾
|
||||
if (response_frame[4] != 0x68 || response_frame[11] != 0x68 || response_frame[TOTAL_RECEIVE_FRAME_LEN + data_info->byte_size - 1] != 0x16) {
|
||||
printf("Invalid frame format for index %d\n", i);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// 校验和
|
||||
uint8_t calc_cs = CheckSum(response_frame + 4, BASE_RECEIVE_FRAME_LEN + data_info->byte_size - 2);
|
||||
uint8_t recv_cs = response_frame[TOTAL_RECEIVE_FRAME_LEN + data_info->byte_size - 2];
|
||||
if (calc_cs != recv_cs) {
|
||||
printf("CheckSum error at index %d: calc %02X, recv %02X\n", i, calc_cs, recv_cs);
|
||||
is_success = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
ParseResponseFrame(response_frame, data_info, i, root);
|
||||
|
||||
PrivTaskDelay(SAMPLE_DATA_INTERVAL_MS);
|
||||
}
|
||||
|
||||
if (!is_success) {
|
||||
printf("read all data failed\n");
|
||||
PrivFree(data_frame_ptr);
|
||||
cJSON_Delete(root);
|
||||
continue;
|
||||
}
|
||||
|
||||
snprintf((char *)data_frame_ptr->id, sizeof(data_frame_ptr->id), "%s %s", parsed_date, parsed_time);
|
||||
printf("data_frame_ptr->id: %s\n", data_frame_ptr->id);
|
||||
|
||||
char *json_str = cJSON_Print(root);
|
||||
strncpy((char *)data_frame_ptr->data, json_str, MAX_DATA_SIZE - 1);
|
||||
data_frame_ptr->data[MAX_DATA_SIZE - 1] = '\0'; // 确保结尾是 \0
|
||||
printf("data_frame_ptr->data: %s\n", data_frame_ptr->data);
|
||||
|
||||
// 删除字符串空间
|
||||
free(json_str);
|
||||
// 删除 cJSON 对象
|
||||
cJSON_Delete(root);
|
||||
|
||||
/* 将解析后的数据帧放入循环队列 */
|
||||
PrivMutexObtain(&queue_buffer_ptr->mutex); // 获取互斥锁
|
||||
OfferBuffer(queue_buffer_ptr, data_frame_ptr); // 将数据帧放入队列
|
||||
printf("receive data from DTZ178, id: %s\n", data_frame_ptr->id); // 打印接收到的数据帧ID
|
||||
PrivMutexAbandon(&queue_buffer_ptr->mutex); // 释放互斥锁
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量,即告知发送数据线程,队列中有新的数据帧
|
||||
|
||||
PrivTaskDelay(RECEIVE_DATA_INTERVAL_MS); // 延迟一段时间再读取下一帧数据
|
||||
|
||||
printf("end cycle\n");
|
||||
}
|
||||
|
||||
PrivClose(fd); // 关闭设备文件
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过4G向服务器发送数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *SendDataToServerTask_4G(void *arg) {
|
||||
uint8_t server_ip_address[16] = {}; // 目的IP地址
|
||||
uint8_t server_port[6] = {}; // 目的端口号
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
unsigned char receive_buffer[256]; // 从服务器接收每帧响应的存储空间
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); // 查找4G模块适配器
|
||||
|
||||
AdapterDeviceOpen(adapter); // 打开适配器对应的设备(实际打开串口中断)
|
||||
int baud_rate = BAUD_RATE_115200; // 波特率,用于设置4G模块串口
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate); // 对适配器对应设备进行配置(实际配置波特率)
|
||||
|
||||
struct DataFrame *data_frame_ptr = NULL; // 数据帧定义
|
||||
while (1) {
|
||||
PrivSemaphoreObtainWait(&queue_buffer_ptr->full, NULL); // 尝试获取循环队列队头元素,如果获取信号量失败,则等待信号量
|
||||
#ifdef BSP_BLE_CONFIG // 如果启用了BLE配置功能
|
||||
/* 获取互斥锁 */
|
||||
PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待
|
||||
|
||||
/* 尝试连接服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_4G[1],
|
||||
CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_4G*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
if (CFG->mqttSwitch_4G == 1) { // 如果使能MQTT
|
||||
AdapterDeviceMqttConnect(adapter, server_ip_address, server_port, CFG->mqttClientId_4G, CFG->mqttUsername_4G,
|
||||
CFG->mqttPassword_4G);
|
||||
} else { // 如果禁用MQTT
|
||||
AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4);
|
||||
}
|
||||
|
||||
AdapterDeviceNetstat(adapter); // 读取网络连接状态
|
||||
/* 若连接失败,则等待10s再次尝试连接 */
|
||||
if (CFG->mqttSwitch_4G == 0 && !adapter->network_info.is_connected ||
|
||||
CFG->mqttSwitch_4G == 1 && !adapter->network_info.mqttIsConnected) {
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
printf("4G connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#else // 如果没有启用BLE配置功能
|
||||
/* 尝试连接到服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_4G[1],
|
||||
CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_4G*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int reconnect_count = RECONNECT_COUNT; // 尝试重新连接服务器最多RECONNECT_COUNT次
|
||||
while (reconnect_count > 0) {
|
||||
int res;
|
||||
if (CFG->mqttSwitch_4G == 1) {
|
||||
res = AdapterDeviceMqttConnect(adapter, mqttServerIp, mqttServerPort, CFG->mqttClientId_4G, CFG->mqttUsername_4G,
|
||||
CFG->mqttPassword_4G);
|
||||
} else {
|
||||
res = AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4);
|
||||
}
|
||||
if (res == 0) {
|
||||
break;
|
||||
}
|
||||
reconnect_count--;
|
||||
}
|
||||
if (reconnect_count <= 0) { // 若RECONNECT_COUNT次都连接失败,则等待10s再次尝试连接
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
printf("4G connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
PrivMutexObtain(&queue_buffer_ptr->mutex); // 获取互斥锁
|
||||
data_frame_ptr = PollBuffer(queue_buffer_ptr); // 从队列中获取数据帧
|
||||
PrivMutexAbandon(&queue_buffer_ptr->mutex); // 释放互斥锁
|
||||
|
||||
int resend_count = RESEND_COUNT; // 定义数据帧重发次数
|
||||
while (data_frame_ptr != NULL && resend_count > 0) { // 只有数据帧非空并且还有剩余重发次数,才进行发送
|
||||
/* 向服务器发送数据 */
|
||||
printf("data_frame_ptr->data: %s", data_frame_ptr->data);
|
||||
printf("send data to server, id: %s\n", data_frame_ptr->id);
|
||||
if (CFG->mqttSwitch_4G == 1) { // MQTT模式下,无需服务器响应数据
|
||||
AdapterDeviceMqttSend(adapter, CFG->mqttTopic_4G, data_frame_ptr->data,
|
||||
strlen(data_frame_ptr->data)); // 发送数据,注意当前最多发送256字节
|
||||
break;
|
||||
} else {
|
||||
AdapterDeviceSend(adapter, data_frame_ptr->data,
|
||||
strlen(data_frame_ptr->data)); // 发送数据,注意当前最多发送256字节
|
||||
|
||||
/* 从服务器接收响应,约定服务器接收完数据帧后,返回数据帧中的前12个字节,即数据帧id */
|
||||
/* 多读取2字节,是为了防止前面还有命令模式返回的剩余的\r\n影响判断 */
|
||||
memset(receive_buffer, 0, sizeof(receive_buffer));
|
||||
int receive_length = AdapterDeviceRecv(adapter, receive_buffer, strlen(data_frame_ptr->id) + 2);
|
||||
if (receive_length == strlen(data_frame_ptr->id) + 2 || receive_length == strlen(data_frame_ptr->id)) {
|
||||
/* 打印服务器响应 */
|
||||
printf("receive_length: %d\n", receive_length);
|
||||
printf("receive_buffer: ");
|
||||
for (int i = 0; i < receive_length; i++) {
|
||||
printf("%c", receive_buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
/* 比较服务器响应的内容与发送的数据帧id是否一致 */
|
||||
if (strstr(receive_buffer, data_frame_ptr->id) != NULL) {
|
||||
break; // 接收成功,退出循环
|
||||
}
|
||||
} else {
|
||||
printf("receive_length: %d\n", receive_length);
|
||||
printf("receive_buffer: ");
|
||||
for (int i = 0; i < receive_length; i++) {
|
||||
printf("%d ", receive_buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
resend_count--;
|
||||
}
|
||||
if (data_frame_ptr != NULL) {
|
||||
PrivFree(data_frame_ptr); // 释放数据帧内存
|
||||
data_frame_ptr = NULL; // 避免野指针
|
||||
}
|
||||
// AdapterDeviceDisconnect(adapter, NULL); // 关闭适配器对应的设备
|
||||
#ifdef BSP_BLE_CONFIG
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
#endif
|
||||
if (resend_count <= 0) { // 如果数据帧重发次数超过上限,表示发送失败,丢弃该帧
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 通过以太网向服务器发送数据的线程
|
||||
* @param arg 循环队列指针
|
||||
* @return void* 目前返回值无意义
|
||||
*/
|
||||
static void *SendDataToServerTask_Ethernet(void *arg) {
|
||||
uint8_t server_ip_address[16] = {}; // 目的IP地址
|
||||
uint8_t server_port[6] = {}; // 目的端口号
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)arg; // 循环队列指针
|
||||
unsigned char receive_buffer[256]; // 从服务器接收每帧响应的存储空间
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME); // 查找以太网模块适配器
|
||||
|
||||
#ifndef BSP_BLE_CONFIG // 如果没有使能蓝牙配置功能
|
||||
AdapterDeviceSetUp(adapter); // 启动以太网主任务线程
|
||||
AdapterDeviceSetDhcp(adapter, CFG->dhcpSwitch_Ethernet); // 启用或禁用DHCP
|
||||
#endif
|
||||
|
||||
struct DataFrame *data_frame_ptr = NULL; // 数据帧定义
|
||||
while (1) {
|
||||
PrivSemaphoreObtainWait(&queue_buffer_ptr->full, NULL); // 尝试获取循环队列队头元素,如果获取信号量失败,则等待信号量
|
||||
#ifdef BSP_BLE_CONFIG // 使能蓝牙配置功能
|
||||
/* 获取互斥锁 */
|
||||
PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待
|
||||
PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待;
|
||||
|
||||
/* 尝试连接服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1],
|
||||
CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_Ethernet*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int res = AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4);
|
||||
|
||||
/* 连接失败,则等待10s再次尝试连接 */
|
||||
if (res != 0 && res != 0x1D) {
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
printf("Ethernet connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
/* 尝试连接到服务器 */
|
||||
sprintf(server_ip_address, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1],
|
||||
CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]);
|
||||
sprintf(server_port, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8);
|
||||
printf("-*-*-*-*sendDataToServerTask_Ethernet*-*-*-*\n");
|
||||
printf("server_ip_address:\t%s\n", server_ip_address);
|
||||
printf("server_port:\t\t%s\n", server_port);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
int reconnect_count = RECONNECT_COUNT; // 尝试重新连接服务器最多RECONNECT_COUNT次
|
||||
while (reconnect_count > 0) {
|
||||
int res = AdapterDeviceConnect(adapter, CLIENT, server_ip_address, server_port, IPV4); // 尝试连接服务器
|
||||
if (res == 0 || res == 0x1D) {
|
||||
break;
|
||||
}
|
||||
reconnect_count--;
|
||||
}
|
||||
if (reconnect_count <= 0) { // 若RECONNECT_COUNT次都连接失败,则等待10s再次尝试连接
|
||||
PrivSemaphoreAbandon(&queue_buffer_ptr->full); // 释放信号量
|
||||
printf("Ethernet connect to server failed\n"); // 连接失败,打印错误信息
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
PrivMutexObtain(&queue_buffer_ptr->mutex); // 获取互斥锁
|
||||
data_frame_ptr = PollBuffer(queue_buffer_ptr); // 从队列中获取数据帧
|
||||
PrivMutexAbandon(&queue_buffer_ptr->mutex); // 释放互斥锁
|
||||
|
||||
int resend_count = RESEND_COUNT; // 定义数据帧重发次数
|
||||
|
||||
/* 只有数据帧非空并且还有剩余重发次数,才进行发送 */
|
||||
while (data_frame_ptr != NULL && resend_count > 0) {
|
||||
/* 向服务器发送数据 */
|
||||
printf("send data to server, id: %s\n", data_frame_ptr->id);
|
||||
printf("data_frame_ptr->data: %s", data_frame_ptr->data);
|
||||
AdapterDeviceSend(adapter, data_frame_ptr->data,
|
||||
strlen(data_frame_ptr->data)); // 发送数据,注意当前最多发送256字节
|
||||
|
||||
/* 从服务器接收响应,约定服务器接收完数据帧后,返回数据帧中的前12个字节,即数据帧id */
|
||||
memset(receive_buffer, 0, sizeof(receive_buffer));
|
||||
PrivTaskDelay(6000);
|
||||
if (AdapterDeviceRecv(adapter, receive_buffer, strlen(data_frame_ptr->id)) == strlen(data_frame_ptr->id)) {
|
||||
/* 打印服务器响应 */
|
||||
printf("receive_buffer: ");
|
||||
for (int i = 0; i < strlen(receive_buffer); i++) {
|
||||
printf("%c", receive_buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
/* 比较服务器响应的内容与发送的数据帧id是否一致 */
|
||||
if (strstr(data_frame_ptr->id, receive_buffer) != NULL) {
|
||||
break; // 接收成功,退出循环
|
||||
}
|
||||
}
|
||||
resend_count--;
|
||||
}
|
||||
if (data_frame_ptr != NULL) {
|
||||
PrivFree(data_frame_ptr); // 释放数据帧内存
|
||||
data_frame_ptr = NULL; // 避免野指针
|
||||
}
|
||||
AdapterDeviceDisconnect(adapter, NULL);
|
||||
#ifdef BSP_BLE_CONFIG
|
||||
/* 释放互斥锁 */
|
||||
PrivMutexAbandon(&romConfigurationMutex);
|
||||
PrivMutexAbandon(&adapter->lock);
|
||||
#endif
|
||||
if (resend_count <= 0) { // 如果数据帧重发次数超过上限,表示发送失败,丢弃该帧
|
||||
PrivTaskDelay(1000 * 10); // 延迟10秒,避免网络拥塞
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 开启从DTZ178接收数据的线程以及上传数据到服务器的线程,此方法在main方法中被调用,开机或复位启动
|
||||
*/
|
||||
void StartUpTransformDataTask(void) {
|
||||
/* 分配循环队列空间 */
|
||||
struct QueueBuffer *queue_buffer_ptr = (struct QueueBuffer *)PrivCalloc(1, sizeof(struct QueueBuffer));
|
||||
if (InitBuffer(queue_buffer_ptr) < 0) {
|
||||
PrivFree(queue_buffer_ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 启动从DTZ178接收数据的线程 */
|
||||
pthread_attr_t receive_data_from_dtz178_task_attr;
|
||||
pthread_args_t receive_data_from_dtz178_task_args;
|
||||
receive_data_from_dtz178_task_attr.schedparam.sched_priority = 16; // 线程优先级
|
||||
receive_data_from_dtz178_task_attr.stacksize = 2048; // 线程栈大小
|
||||
receive_data_from_dtz178_task_args.pthread_name = "ReceiveDataFromDTZ178Task"; // 线程名字
|
||||
receive_data_from_dtz178_task_args.arg = queue_buffer_ptr; // 线程参数
|
||||
pthread_t receive_data_thread; // 线程ID
|
||||
PrivTaskCreate(&receive_data_thread, &receive_data_from_dtz178_task_attr, ReceiveDataFromDTZ178Task, &receive_data_from_dtz178_task_args);
|
||||
PrivTaskStartup(&receive_data_thread);
|
||||
|
||||
/* 启动上传数据到服务器的线程 */
|
||||
pthread_attr_t send_data_to_server_task_attr;
|
||||
pthread_args_t send_data_to_server_task_args;
|
||||
send_data_to_server_task_attr.schedparam.sched_priority = 16; // 线程优先级
|
||||
send_data_to_server_task_attr.stacksize = 2200; // 线程栈大小
|
||||
send_data_to_server_task_args.pthread_name = "SendDataToServerTask"; // 线程名字
|
||||
send_data_to_server_task_args.arg = queue_buffer_ptr; // 线程参数
|
||||
pthread_t send_data_thread; // 线程ID
|
||||
void *(*start_routine)(void *) = SendDataToServerTask_4G; // 通过4G模块上传到服务器
|
||||
// void *(*start_routine)(void *) = SendDataToServerTask_Ethernet; // 通过以太网模块上传到服务器
|
||||
PrivTaskCreate(&send_data_thread, &send_data_to_server_task_attr, start_routine, &send_data_to_server_task_args); // 通过4G模块上传到服务器
|
||||
PrivTaskStartup(&send_data_thread);
|
||||
}
|
||||
|
|
@ -8,20 +8,6 @@ menu "connection app"
|
|||
menuconfig SOCKET_DEMO
|
||||
bool "Config test socket demo"
|
||||
default n
|
||||
|
||||
choice
|
||||
prompt "Select Ammeter Device Type"
|
||||
default DEVICE_ADL400
|
||||
|
||||
config DEVICE_ADL400
|
||||
bool "CH32V208_ADL400"
|
||||
|
||||
config DEVICE_DTZ178
|
||||
bool "CH32V208_DTZ178"
|
||||
|
||||
config DEVICE_DTSD342
|
||||
bool "CH32V208_DTSD342"
|
||||
endchoice
|
||||
endif
|
||||
|
||||
endmenu
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
SRC_DIR := advantech beckhoff br delta mitsubishi omron schneider siemens ge xinje inovance keyence panasonic fatek ab abb koyo
|
||||
SRC_DIR := advantech beckhoff br delta mitsubishi omron schneider siemens ge xinje inovance keyence panasonic fatek ab abb
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
SRC_FILES := inovance_am401_cpu1608tn_ethernet.c inovance_am401_cpu1608tn_uart.c inovance_H3U_cpu3232MT_ethernet.c
|
||||
SRC_FILES := inovance_am401_cpu1608tn_ethernet.c inovance_am401_cpu1608tn_uart.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2024 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file inovance_H3U_cpu3232MT_ethernet.c
|
||||
* @brief PLC Inovance H3U-3232MT app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2024.08.06
|
||||
*/
|
||||
|
||||
#include <control.h>
|
||||
|
||||
void ControlInovanceH3UCPU3232MTTest(void)
|
||||
{
|
||||
int i, j = 0;
|
||||
int read_data_length = 0;
|
||||
uint8_t read_data[128] = {0};
|
||||
ControlProtocolType modbus_tcp_protocol = ControlProtocolFind();
|
||||
if (NULL == modbus_tcp_protocol) {
|
||||
printf("%s get modbus tcp protocol %p failed\n", __func__, modbus_tcp_protocol);
|
||||
return;
|
||||
}
|
||||
printf("%s get modbus tcp protocol %p successfull\n", __func__, modbus_tcp_protocol);
|
||||
|
||||
if (CONTROL_REGISTERED == modbus_tcp_protocol->protocol_status) {
|
||||
ControlProtocolOpen(modbus_tcp_protocol);
|
||||
for (;;) {
|
||||
read_data_length = ControlProtocolRead(modbus_tcp_protocol, read_data, sizeof(read_data));
|
||||
printf("%s read [%d] modbus tcp data %d using receipe file\n", __func__, i, read_data_length);
|
||||
if (read_data_length) {
|
||||
for (j = 0; j < read_data_length; j++) {
|
||||
printf("j %d data 0x%x\n", j, read_data[j]);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
memset(read_data, 0, sizeof(read_data));
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
// ControlProtocolClose(modbus_tcp_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlInovanceH3UCPU3232MTTest, Inovance PLC N3UCPU3232MT Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
{
|
||||
"device_id": 1,
|
||||
"device_name": "Ino_H3U3232MT",
|
||||
"communication_type": 0,
|
||||
"socket_config": {
|
||||
"plc_ip": "192.168.250.55",
|
||||
"local_ip": "192.168.250.147",
|
||||
"gateway": "192.168.250.252",
|
||||
"netmask": "255.255.255.0",
|
||||
"port": 502
|
||||
},
|
||||
"protocol_type": 2,
|
||||
"read_period": 300,
|
||||
"read_item_list": [
|
||||
{
|
||||
"value_name": "M8000",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address": 8000,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D120",
|
||||
"value_type": 3,
|
||||
"function_code": 3,
|
||||
"start_address": 120,
|
||||
"quantity": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.7 MiB |
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2024 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file koyo_nk1cpu40.c
|
||||
* @brief PLC AB MICRO850 app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2024.07.03
|
||||
*/
|
||||
|
||||
#include <control.h>
|
||||
|
||||
void ControlKoyoNK1CPU40Test(void)
|
||||
{
|
||||
int i, j = 0;
|
||||
int read_data_length = 0;
|
||||
uint8_t read_data[128] = {0};
|
||||
ControlProtocolType modbus_tcp_protocol = ControlProtocolFind();
|
||||
if (NULL == modbus_tcp_protocol) {
|
||||
printf("%s get modbus tcp protocol %p failed\n", __func__, modbus_tcp_protocol);
|
||||
return;
|
||||
}
|
||||
printf("%s get modbus tcp protocol %p successfull\n", __func__, modbus_tcp_protocol);
|
||||
|
||||
if (CONTROL_REGISTERED == modbus_tcp_protocol->protocol_status) {
|
||||
ControlProtocolOpen(modbus_tcp_protocol);
|
||||
for (;;) {
|
||||
read_data_length = ControlProtocolRead(modbus_tcp_protocol, read_data, sizeof(read_data));
|
||||
printf("%s read [%d] modbus tcp data %d using receipe file\n", __func__, i, read_data_length);
|
||||
if (read_data_length) {
|
||||
for (j = 0; j < read_data_length; j++) {
|
||||
printf("j %d data 0x%x\n", j, read_data[j]);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
memset(read_data, 0, sizeof(read_data));
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
// ControlProtocolClose(modbus_tcp_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlKoyoNK1CPU40Test, Koyo Plc NK1CPU40 Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
|
@ -15,18 +15,9 @@
|
|||
// #include <user_api.h>
|
||||
#include <transform.h>
|
||||
|
||||
#ifdef CONFIG_LIB_USING_PIKAPYTHON
|
||||
#include "../lib/pikapython/pikascript-api/pikaScript.h"
|
||||
#endif
|
||||
|
||||
extern int FrameworkInit();
|
||||
extern void ApplicationOtaTaskInit(void);
|
||||
|
||||
#ifdef CONFIG_LIB_USING_PIKAPYTHON
|
||||
/* PikaPython application interfaces */
|
||||
extern PikaObj* pikaPythonInit(void);
|
||||
#endif
|
||||
|
||||
#ifdef OTA_BY_PLATFORM
|
||||
extern int OtaTask(void);
|
||||
#endif
|
||||
|
|
@ -39,17 +30,6 @@ int main(void)
|
|||
{
|
||||
printf("\nHello, world!\n");
|
||||
FrameworkInit();
|
||||
|
||||
#ifdef CONFIG_LIB_USING_PIKAPYTHON
|
||||
/* Initialize and run PikaPython */
|
||||
printf("\n initializing PikaPython...\n");
|
||||
PikaObj* pikaMain = pikaPythonInit();
|
||||
if (pikaMain != NULL) {
|
||||
printf("\n PikaPython test completed successfully!\n");
|
||||
} else {
|
||||
printf("\n PikaPython initialization failed!\n");
|
||||
}
|
||||
#endif
|
||||
#ifdef APPLICATION_OTA
|
||||
ApplicationOtaTaskInit();
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,11 +1,3 @@
|
|||
config ADAPTER_EC801E
|
||||
bool "Using 4G adapter device EC801E"
|
||||
default n
|
||||
|
||||
if ADAPTER_EC801E
|
||||
source "$APP_DIR/Framework/connection/4g/ec801e/Kconfig"
|
||||
endif
|
||||
|
||||
config ADAPTER_EC200T
|
||||
bool "Using 4G adapter device EC200T"
|
||||
default n
|
||||
|
|
@ -21,11 +13,3 @@ config ADAPTER_EC200A
|
|||
if ADAPTER_EC200A
|
||||
source "$APP_DIR/Framework/connection/4g/ec200a/Kconfig"
|
||||
endif
|
||||
|
||||
config ADAPTER_GM800TF
|
||||
bool "Using 4G adapter device GM800TF"
|
||||
default n
|
||||
|
||||
if ADAPTER_GM800TF
|
||||
source "$APP_DIR/Framework/connection/4g/gm800tf/Kconfig"
|
||||
endif
|
||||
|
|
|
|||
|
|
@ -9,10 +9,6 @@ endif
|
|||
ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
SRC_FILES := adapter_4g.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_EC801E),y)
|
||||
SRC_DIR += ec801e
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_EC200T),y)
|
||||
SRC_DIR += ec200t
|
||||
endif
|
||||
|
|
@ -21,9 +17,5 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
|||
SRC_DIR += ec200a
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_GM800TF),y)
|
||||
SRC_DIR += gm800tf
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
endif
|
||||
|
|
@ -28,14 +28,6 @@ extern AdapterProductInfoType Ec200tAttach(struct Adapter *adapter);
|
|||
extern AdapterProductInfoType Ec200aAttach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_GM800TF
|
||||
extern AdapterProductInfoType Gm800tfAttach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_EC801E
|
||||
extern AdapterProductInfoType Ec801eAttach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
static int Adapter4GRegister(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
|
|
@ -74,20 +66,6 @@ int Adapter4GInit(void)
|
|||
return -1;
|
||||
}
|
||||
|
||||
#ifdef ADAPTER_EC801E
|
||||
AdapterProductInfoType product_info = Ec801eAttach(adapter);
|
||||
if (!product_info) {
|
||||
printf("Adapter4GInit ec801e attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_EC200T
|
||||
AdapterProductInfoType product_info = Ec200tAttach(adapter);
|
||||
if (!product_info) {
|
||||
|
|
@ -114,20 +92,6 @@ int Adapter4GInit(void)
|
|||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_GM800TF
|
||||
AdapterProductInfoType product_info = Gm800tfAttach(adapter);
|
||||
if (!product_info) {
|
||||
printf("Adapter4GInit gm800tf attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
|
|
@ -200,144 +164,6 @@ int Adapter4GTest(void)
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_EC801E
|
||||
/* Using Public TCP server to test 4G Socket connection */
|
||||
uint8 server_addr[64] = "xyheqmx.f1.luyouxia.net";
|
||||
uint8 server_port[64] = "17028";
|
||||
uint8 client_id[64] = "quectel";
|
||||
uint8 username[64] = "test";
|
||||
uint8 password[64] = "test123456";
|
||||
uint8 topic_pub[64] = "/reply";
|
||||
uint8 topic_sub[64] = "/get";
|
||||
|
||||
adapter->socket.socket_id = 0;
|
||||
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
|
||||
// AdapterDeviceNetstat(adapter);
|
||||
|
||||
/*4G TCP Connect Test*/
|
||||
AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
|
||||
while (1) {
|
||||
AdapterDeviceSend(adapter, send_msg, strlen(send_msg));
|
||||
AdapterDeviceRecv(adapter, recv_msg, 256);
|
||||
printf("4G recv msg %s\n", recv_msg);
|
||||
memset(recv_msg, 0, 256);
|
||||
}
|
||||
|
||||
/*4G MQTT Connect Test*/
|
||||
// AdapterDeviceMqttConnect(adapter, server_addr, server_port, client_id, username, password);
|
||||
|
||||
// while (1) {
|
||||
// AdapterDeviceMqttSend(adapter, topic_pub, send_msg, strlen(send_msg));
|
||||
// AdapterDeviceMqttRecv(adapter, topic_sub, recv_msg, 256);
|
||||
// printf("4G mqtt recv msg %s\n", recv_msg);
|
||||
// memset(recv_msg, 0, 256);
|
||||
// }
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTER_GM800TF
|
||||
uint8 server_addr[64] = "115.238.53.59";
|
||||
uint8 server_port[64] = "10208";
|
||||
|
||||
adapter->socket.socket_id = 0;
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
// AdapterDeviceDisconnect(adapter, NULL);
|
||||
// AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
AdapterDeviceNetstat(adapter);
|
||||
|
||||
// char sendData[15] = "Hello World!";
|
||||
char sendData = 'a';
|
||||
char receiveData = 0;
|
||||
int failCount = 0;
|
||||
for (int i = 0; i < 1024; i++) { // send 1kB data
|
||||
AdapterDeviceSend(adapter, &sendData, 1);
|
||||
sendData = sendData + 1 > 'z' ? 'a' : sendData + 1;
|
||||
}
|
||||
// AdapterDeviceSend(adapter, sendData, 13);
|
||||
// while (1) {
|
||||
// // if (sendData > 'z') {
|
||||
// // break;
|
||||
// // }
|
||||
// AdapterDeviceSend(adapter, &sendData, 1);
|
||||
// sendData = sendData + 1 > 'z' ? 'a' : sendData + 1;
|
||||
// // int ret = AdapterDeviceRecv(adapter, &receiveData, 1);
|
||||
// // printf("receiveData: %d\n", receiveData);
|
||||
// // if (ret >= 0 && receiveData == sendData) {
|
||||
// // sendData = sendData + 1 > 'z' ? 'a' : sendData + 1;
|
||||
// // failCount = 0;
|
||||
// // } else {
|
||||
// // failCount++;
|
||||
// // if (failCount >= 10) {
|
||||
// // AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
// // failCount = 0;
|
||||
// // }
|
||||
// // }
|
||||
// // printf("4G recv msg %c\n", receiveData);
|
||||
// // receiveData = 0;
|
||||
// }
|
||||
// PrivTaskDelay(10000);
|
||||
AdapterDeviceClose(adapter);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(Adapter4GTest, a LTE adapter sample, PRIV_SHELL_CMD_FUNC_ATTR);
|
||||
|
||||
#ifdef ADAPTER_GM800TF
|
||||
// unsigned char data[1024 * 40];
|
||||
void *uploadDataTask(void *param) {
|
||||
int baud_rate = BAUD_RATE_115200;
|
||||
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME);
|
||||
int reconnectLimit = 3; // try reconnect to server up to 3 times
|
||||
|
||||
uint8 server_addr[64] = "115.238.53.59";
|
||||
uint8 server_port[64] = "10208";
|
||||
|
||||
adapter->socket.socket_id = 0;
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
|
||||
/* try to connect to server */
|
||||
do {
|
||||
AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
AdapterDeviceNetstat(adapter);
|
||||
if (adapter->network_info.is_connected && adapter->network_info.signal_strength < 99) {
|
||||
break;
|
||||
}
|
||||
} while (--reconnectLimit > 0);
|
||||
if (reconnectLimit <= 0) {
|
||||
printf("4G connect to server failed\n");
|
||||
AdapterDeviceClose(adapter);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* send data to server */
|
||||
char sendData[15] = "Hello World!";
|
||||
AdapterDeviceSend(adapter, &sendData, 13);
|
||||
// char sendData = 'a';
|
||||
// char receiveData = 0;
|
||||
// int failCount = 0;
|
||||
// for (int i = 0; i < 1024; i++) { // send 1kB data
|
||||
// AdapterDeviceSend(adapter, &sendData, 1);
|
||||
// sendData = sendData + 1 > 'z' ? 'a' : sendData + 1;
|
||||
// }
|
||||
|
||||
AdapterDeviceClose(adapter);
|
||||
}
|
||||
|
||||
void startUploadDataTask(void) {
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 20;
|
||||
attr.stacksize = 8096;
|
||||
|
||||
// char task_name[] = "upload_data_task";
|
||||
|
||||
pthread_t thread;
|
||||
PrivTaskCreate(&thread, &attr, uploadDataTask, NULL);
|
||||
PrivTaskStartup(&thread);
|
||||
}
|
||||
#endif
|
||||
PRIV_SHELL_CMD_FUNCTION(Adapter4GTest, a EC200T or EC200A adapter sample, PRIV_SHELL_CMD_FUNC_ATTR);
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@
|
|||
#include <at_agent.h>
|
||||
|
||||
#define EC200A_AT_MODE_CMD "+++"
|
||||
#define EC200A_DATA_MODE_CMD "ATO\r\n"
|
||||
#define EC200A_GET_QCCID_CMD "AT+QCCID\r\n"
|
||||
#define EC200A_GET_CPIN_CMD "AT+CPIN?\r\n"
|
||||
#define EC200A_GET_CREG_CMD "AT+CREG?\r\n"
|
||||
|
|
@ -35,7 +34,6 @@
|
|||
#define EC200A_GET_COPS_CMD "AT+COPS?\r\n"
|
||||
#define EC200A_GET_CSQ_CMD "AT+CSQ\r\n"
|
||||
#define EC200A_GET_POP_IP "AT+CGPADDR=1\r\n"
|
||||
#define EC200A_CONNECTION_STATUS_CMD "AT+QISTATE?\r\n"
|
||||
|
||||
#define EC200A_OK_REPLY "OK"
|
||||
#define EC200A_READY_REPLY "READY"
|
||||
|
|
@ -97,7 +95,7 @@ static int Ec200aOpen(struct Adapter *adapter)
|
|||
/*step2: init AT agent*/
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "4G_uart_client";
|
||||
if (0 != InitATAgent(agent_name, adapter->fd, 256)) {
|
||||
if (0 != InitATAgent(agent_name, adapter->fd, 512)) {
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -124,7 +122,6 @@ static int Ec200aClose(struct Adapter *adapter)
|
|||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); //'O', 'K'
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
|
|
@ -207,7 +204,6 @@ static int Ec200aConnect(struct Adapter *adapter, enum NetRoleType net_role, con
|
|||
uint8_t ec200a_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
|
|
@ -325,8 +321,6 @@ static int Ec200aConnect(struct Adapter *adapter, enum NetRoleType net_role, con
|
|||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.is_connected = 1;
|
||||
|
||||
ADAPTER_DEBUG("Ec200a connect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
|
@ -364,7 +358,6 @@ static int Ec200aDisconnect(struct Adapter *adapter)
|
|||
uint8_t ec200a_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
|
|
@ -379,8 +372,6 @@ static int Ec200aDisconnect(struct Adapter *adapter)
|
|||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.is_connected = 0;
|
||||
|
||||
ADAPTER_DEBUG("Ec200a disconnect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
|
@ -423,7 +414,6 @@ static int Ec200aNetstat(struct Adapter *adapter) {
|
|||
int try = 0;
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
|
|
@ -478,7 +468,7 @@ static int Ec200aNetstat(struct Adapter *adapter) {
|
|||
extractCarrierInfo(result, &info);
|
||||
adapter->network_info.carrier_type = info.carrier_type;
|
||||
|
||||
/*step6: serial write "AT+CSQ", get signal strength*/
|
||||
/*step6: serial write "AT+CSQ", get carrier type*/
|
||||
memset(result, 0, sizeof(result));
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
|
|
@ -518,29 +508,6 @@ static int Ec200aNetstat(struct Adapter *adapter) {
|
|||
goto out;
|
||||
}
|
||||
|
||||
/*step8: serial write "AT+QISTATE", get connection state*/
|
||||
memset(result, 0, sizeof(result));
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, EC200A_CONNECTION_STATUS_CMD, result);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (strstr(result, "+QISTATE:") != NULL) {
|
||||
printf("Socket is connected\n");
|
||||
adapter->network_info.is_connected = 1;
|
||||
} else {
|
||||
printf("Socket is disconnected\n");
|
||||
adapter->network_info.is_connected = 0;
|
||||
}
|
||||
|
||||
// return to data mode
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "ATO\r\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ int Ec200aMqttConnect(struct Adapter *adapter, const char *ip, const char *port,
|
|||
uint8_t ec200a_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
|
|
@ -136,8 +135,6 @@ int Ec200aMqttConnect(struct Adapter *adapter, const char *ip, const char *port,
|
|||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.mqttIsConnected = 1;
|
||||
|
||||
ADAPTER_DEBUG("Ec200a mqtt connect done\n");
|
||||
|
||||
|
|
@ -154,7 +151,6 @@ int Ec200aMqttDisconnect(struct Adapter *adapter)
|
|||
int ret = 0;
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
|
|
@ -167,8 +163,6 @@ int Ec200aMqttDisconnect(struct Adapter *adapter)
|
|||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.mqttIsConnected = 0;
|
||||
|
||||
ADAPTER_DEBUG("Ec200a disconnect mqtt done\n");
|
||||
|
||||
return 0;
|
||||
|
|
@ -185,7 +179,6 @@ int Ec200aMqttSend(struct Adapter *adapter, const char *topic, const void *buf,
|
|||
uint8_t ec200a_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x3E, 0x20);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
char len_str[10];
|
||||
sprintf(len_str, "%u", len);
|
||||
|
|
@ -223,7 +216,6 @@ int Ec200aMqttRecv(struct Adapter *adapter, const char *topic, void *buf, size_t
|
|||
uint8_t ec200a_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
memset(ec200a_cmd, 0, sizeof(ec200a_cmd));
|
||||
strcpy(ec200a_cmd, EC200A_SET_MQTT_SUB_CMD);
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
config ADAPTER_4G_EC801E
|
||||
string "EC801E adapter name"
|
||||
default "ec801e"
|
||||
|
||||
if ADD_XIZI_FEATURES
|
||||
config ADAPTER_EC801E_DRIVER_EXTUART
|
||||
bool "Using extra uart to support 4G"
|
||||
default n
|
||||
|
||||
config ADAPTER_EC801E_DRIVER
|
||||
string "EC801E device uart driver path"
|
||||
default "/dev/usart6_dev6"
|
||||
depends on !ADAPTER_EC801E_DRIVER_EXTUART
|
||||
|
||||
if ADAPTER_EC801E_DRIVER_EXTUART
|
||||
config ADAPTER_EC801E_DRIVER
|
||||
string "EC801E device extra uart driver path"
|
||||
default "/dev/extuart_dev5"
|
||||
|
||||
config ADAPTER_EC801E_DRIVER_EXT_PORT
|
||||
int "if EC801E device using extuart, choose port"
|
||||
default "5"
|
||||
endif
|
||||
endif
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
############################################################################
|
||||
# APP_Framework/Framework/connection/4g/ec801e/Make.defs
|
||||
############################################################################
|
||||
ifneq ($(CONFIG_ADAPTER_4G_EC801E),)
|
||||
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Framework/connection/4g/ec801e
|
||||
endif
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
include $(KERNEL_ROOT)/.config
|
||||
ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
|
||||
include $(APPDIR)/Make.defs
|
||||
CSRCS += ec801e.c
|
||||
include $(APPDIR)/Application.mk
|
||||
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
SRC_FILES := ec801e.c ec801e_mqtt.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
endif
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
from building import *
|
||||
import os
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = []
|
||||
if GetDepend(['ADAPTER_EC801E']):
|
||||
src += ['ec801e.c']
|
||||
group = DefineGroup('connection 4g ec801e', src, depend = [], CPPPATH = [cwd])
|
||||
|
||||
Return('group')
|
||||
|
|
@ -1,606 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ec801e_cn.c
|
||||
* @brief Implement the connection 4G adapter function, using EC801E-CN device
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2024.12.25
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define EC801E_AT_MODE_CMD "+++"
|
||||
#define EC801E_DATA_MODE_CMD "ATO\r\n"
|
||||
#define EC801E_ATI_CMD "ATI\r\n"
|
||||
#define EC801E_CIMI_CMD "AT+CIMI\r\n"
|
||||
#define EC801E_GET_QCCID_CMD "AT+QCCID=?\r\n"
|
||||
#define EC801E_GET_CPIN_CMD "AT+CPIN?\r\n"
|
||||
#define EC801E_GET_CREG_CMD "AT+CREG?\r\n"
|
||||
#define EC801E_CFG_TCP_CMD "AT+QICSGP"
|
||||
#define EC801E_ACTIVE_PDP_CMD "AT+QIACT=1\r\n"
|
||||
#define EC801E_DEACTIVE_PDP_CMD "AT+QIDEACT=1\r\n"
|
||||
#define EC801E_OPEN_SOCKET_CMD "AT+QIOPEN=1,%u"
|
||||
#define EC801E_CLOSE_SOCKET_CMD "AT+QICLOSE=%u\r\n"
|
||||
#define EC801E_CLOSE "AT+QPOWD\r\n"
|
||||
#define EC801E_GET_COPS_CMD "AT+COPS?\r\n"
|
||||
#define EC801E_GET_CSQ_CMD "AT+CSQ\r\n"
|
||||
#define EC801E_GET_POP_IP "AT+CGPADDR=1\r\n"
|
||||
#define EC801E_CONNECTION_STATUS_CMD "AT+QISTATE?\r\n"
|
||||
|
||||
#define EC801E_OK_REPLY "OK"
|
||||
#define EC801E_READY_REPLY "READY"
|
||||
#define EC801E_CREG_REPLY ",1"
|
||||
#define EC801E_CONNECT_REPLY "CONNECT"
|
||||
|
||||
#define TRY_TIMES 10
|
||||
|
||||
extern int Ec801eMqttConnect(struct Adapter *adapter, const char *ip, const char *port, const char *client_id, const char *username, const char *password);
|
||||
extern int Ec801eMqttDisconnect(struct Adapter *adapter);
|
||||
extern int Ec801eMqttSend(struct Adapter *adapter, const char *topic, const void *buf, size_t len);
|
||||
extern int Ec801eMqttRecv(struct Adapter *adapter, const char *topic, void *buf, size_t len);
|
||||
|
||||
static void Ec801ePowerSet(void)
|
||||
{
|
||||
#ifdef ADAPTER_EC801E_USING_PWRKEY
|
||||
int pin_fd;
|
||||
pin_fd = PrivOpen(ADAPTER_EC801E_PIN_DRIVER, O_RDWR);
|
||||
if (pin_fd < 0) {
|
||||
printf("open %s error\n", ADAPTER_EC801E_PIN_DRIVER);
|
||||
return;
|
||||
}
|
||||
|
||||
struct PinParam pin_param;
|
||||
pin_param.cmd = GPIO_CONFIG_MODE;
|
||||
pin_param.mode = GPIO_CFG_OUTPUT;
|
||||
pin_param.pin = ADAPTER_EC801E_PWRKEY;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = PIN_TYPE;
|
||||
ioctl_cfg.args = &pin_param;
|
||||
PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
|
||||
|
||||
struct PinStat pin_stat;
|
||||
pin_stat.pin = ADAPTER_EC801E_PWRKEY;
|
||||
pin_stat.val = GPIO_LOW; //put power key at low-level state
|
||||
PrivWrite(pin_fd, &pin_stat, 1);
|
||||
|
||||
PrivTaskDelay(2500); //wait at least 2s
|
||||
|
||||
pin_stat.val = GPIO_HIGH; //put power key at high-level state
|
||||
PrivWrite(pin_fd, &pin_stat, 1);
|
||||
|
||||
PrivClose(pin_fd);
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int Ec801eOpen(struct Adapter *adapter)
|
||||
{
|
||||
/*step1: open ec801e serial port*/
|
||||
adapter->fd = PrivOpen(ADAPTER_EC801E_DRIVER, O_RDWR);
|
||||
if (adapter->fd < 0) {
|
||||
printf("Ec801eOpen get serial %s fd error\n", ADAPTER_EC801E_DRIVER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*step2: init AT agent*/
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "4G_uart_client";
|
||||
if (0 != InitATAgent(agent_name, adapter->fd, 256)) {
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
|
||||
adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
ADAPTER_DEBUG("Ec801e open done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Ec801eClose(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t ec801e_cmd[64];
|
||||
|
||||
if (!adapter->agent) {
|
||||
printf("Ec801eClose AT agent NULL\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); //'O', 'K'
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
|
||||
/*step2: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
sprintf(ec801e_cmd, EC801E_CLOSE_SOCKET_CMD, adapter->socket.socket_id);
|
||||
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+QIDEACT", close TCP net before open socket*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_DEACTIVE_PDP_CMD, EC801E_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
/*step4: power down ec801e*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
PrivTaskDelay(12500); //wait at least 12s
|
||||
|
||||
/*step5: close ec801e serial port*/
|
||||
PrivClose(adapter->fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef ADD_RTTHREAD_FEATURES
|
||||
static int Ec801eIoctl(struct Adapter *adapter, int cmd, void *args){ return 0;}
|
||||
#else
|
||||
static int Ec801eIoctl(struct Adapter *adapter, int cmd, void *args)
|
||||
{
|
||||
if (OPE_INT != cmd) {
|
||||
printf("Ec801eIoctl only support OPE_INT, do not support %d\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t baud_rate = *((uint32_t *)args);
|
||||
|
||||
struct SerialDataCfg serial_cfg;
|
||||
memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg));
|
||||
serial_cfg.serial_baud_rate = baud_rate;
|
||||
serial_cfg.serial_data_bits = DATA_BITS_8;
|
||||
serial_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
serial_cfg.serial_parity_mode = PARITY_NONE;
|
||||
serial_cfg.serial_bit_order = STOP_BITS_1;
|
||||
serial_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
#ifdef TOOL_USING_OTA
|
||||
serial_cfg.serial_timeout = OTA_RX_TIMEOUT;
|
||||
#else
|
||||
//serial receive timeout 10s
|
||||
serial_cfg.serial_timeout = 100000;
|
||||
#endif
|
||||
serial_cfg.is_ext_uart = 0;
|
||||
#ifdef ADAPTER_EC801E_DRIVER_EXT_PORT
|
||||
serial_cfg.is_ext_uart = 1;
|
||||
serial_cfg.ext_uart_no = ADAPTER_EC801E_DRIVER_EXT_PORT;
|
||||
serial_cfg.port_configure = PORT_CFG_INIT;
|
||||
#endif
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = &serial_cfg;
|
||||
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
|
||||
|
||||
Ec801ePowerSet();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int Ec801eConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
|
||||
{
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
uint8_t ec801e_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, EC801E_AT_MODE_CMD);
|
||||
PrivTaskDelay(1500); //after +++ command, wait at least 1s
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_ATI_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(300);
|
||||
|
||||
/*step2: serial write "AT+CPIN?", check SIM status*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_CPIN_CMD, EC801E_READY_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(300);
|
||||
|
||||
/*step3: serial write "AT+CCID", get SIM ID*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_QCCID_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+CREG?", check whether registered to GSM net*/
|
||||
PrivTaskDelay(1000); //before CREG command, wait 1s
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_CREG_CMD, EC801E_CREG_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(300);
|
||||
|
||||
/*step5: serial write "AT+QICSGP", connect to China Mobile using ipv4 or ipv6*/
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
|
||||
if (IPV4 == ip_type) {
|
||||
strcpy(ec801e_cmd, "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n");
|
||||
} else if (IPV6 == ip_type) {
|
||||
strcpy(ec801e_cmd, "AT+QICSGP=1,2,\"CMNET\",\"\",\"\",1\r\n");
|
||||
}
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(300);
|
||||
|
||||
/*step6: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
|
||||
sprintf(ec801e_cmd, EC801E_CLOSE_SOCKET_CMD, adapter->socket.socket_id);
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(300);
|
||||
|
||||
/*step7: serial write "AT+QIDEACT", close TCP net before open socket*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_DEACTIVE_PDP_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(300);
|
||||
|
||||
/*step8: serial write "AT+QIACT", open TCP net*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_ACTIVE_PDP_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step9: serial write "AT+QIOPEN", connect socket using TCP*/
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
sprintf(ec801e_cmd, EC801E_OPEN_SOCKET_CMD, adapter->socket.socket_id);
|
||||
strcat(ec801e_cmd, ",\"TCP\",\"");
|
||||
strcat(ec801e_cmd, ip);
|
||||
strcat(ec801e_cmd, "\",");
|
||||
strcat(ec801e_cmd, port);
|
||||
strcat(ec801e_cmd, ",0,2\r\n");
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x43, 0x54);
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_CONNECT_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.is_connected = 1;
|
||||
|
||||
ADAPTER_DEBUG("Ec801e connect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e connect TCP failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int Ec801eSend(struct Adapter *adapter, const void *buf, size_t len)
|
||||
{
|
||||
if (adapter->agent) {
|
||||
EntmSend(adapter->agent, (const char *)buf, len);
|
||||
} else {
|
||||
printf("Ec801eSend can not find agent\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Ec801eRecv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 6);
|
||||
} else {
|
||||
printf("Ec801eRecv can not find agent\n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int Ec801eDisconnect(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t ec801e_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
PrivTaskDelay(1500); //after +++ command, wait at least 1s
|
||||
|
||||
/*step2: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
sprintf(ec801e_cmd, EC801E_CLOSE_SOCKET_CMD, adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.is_connected = 0;
|
||||
|
||||
ADAPTER_DEBUG("Ec801e disconnect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e disconnect TCP failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void extractCarrierInfo(char *response, struct NetworkInfo *networkInfo)
|
||||
{
|
||||
const char *delimiter = "\"";
|
||||
const char *token;
|
||||
|
||||
token = strtok(response, delimiter);
|
||||
token = strtok(NULL, delimiter);
|
||||
|
||||
if (strcmp(token, "CHINA MOBILE") == 0) {
|
||||
networkInfo->carrier_type = CARRIER_CHINA_MOBILE;
|
||||
} else if (strcmp(token, "CHN-UNICOM") == 0) {
|
||||
networkInfo->carrier_type = CARRIER_CHINA_UNICOM;
|
||||
} else if (strcmp(token, "CHN-CT") == 0) {
|
||||
networkInfo->carrier_type = CARRIER_CHINA_TELECOM;
|
||||
} else {
|
||||
networkInfo->carrier_type = CARRIER_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
static int Ec801eNetstat(struct Adapter *adapter) {
|
||||
char result[64] = {0};
|
||||
|
||||
struct NetworkInfo info = {
|
||||
.carrier_type = CARRIER_UNKNOWN,
|
||||
.signal_strength = 0,
|
||||
.ip_address = "192.168.1.1"
|
||||
};
|
||||
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
PrivTaskDelay(1500); //after +++ command, wait at least 1s
|
||||
|
||||
/*step2: serial write "AT+CPIN?", check SIM status*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_CPIN_CMD, EC801E_READY_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+CCID", get SIM ID*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_QCCID_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+CREG?", check whether registered to GSM net*/
|
||||
PrivTaskDelay(1000); //before CREG command, wait 1s
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_CREG_CMD, EC801E_CREG_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step5: serial write "AT+COPS?", get carrier type*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, EC801E_GET_COPS_CMD, result);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
extractCarrierInfo(result, &info);
|
||||
adapter->network_info.carrier_type = info.carrier_type;
|
||||
|
||||
/*step6: serial write "AT+CSQ", get signal strength*/
|
||||
memset(result, 0, sizeof(result));
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, EC801E_GET_CSQ_CMD, result);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(result, "AT+CSQ\n+CSQ: %d", &info.signal_strength) == 1) {
|
||||
printf("Signal Strength: %d\n", info.signal_strength);
|
||||
adapter->network_info.signal_strength = info.signal_strength;
|
||||
} else {
|
||||
printf("Failed to parse signal strength\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step7: serial write "AT+CSQ", get carrier type*/
|
||||
memset(result, 0, sizeof(result));
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, EC801E_GET_POP_IP, result);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(result, "AT+CGPADDR=1\n+CGPADDR: 1,\"%15[^\"]\"", info.ip_address) == 1) {
|
||||
printf("IP Address: %s\n", info.ip_address);
|
||||
strcpy(adapter->network_info.ip_address, info.ip_address);
|
||||
} else {
|
||||
printf("Failed to parse IP address\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step8: serial write "AT+QISTATE", get connection state*/
|
||||
memset(result, 0, sizeof(result));
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, EC801E_CONNECTION_STATUS_CMD, result);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (strstr(result, "+QISTATE:") != NULL) {
|
||||
printf("Socket is connected\n");
|
||||
adapter->network_info.is_connected = 1;
|
||||
} else {
|
||||
printf("Socket is disconnected\n");
|
||||
adapter->network_info.is_connected = 0;
|
||||
}
|
||||
|
||||
// return to data mode
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "ATO\r\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e get netstat failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const struct IpProtocolDone ec801e_done =
|
||||
{
|
||||
.open = Ec801eOpen,
|
||||
.close = Ec801eClose,
|
||||
.ioctl = Ec801eIoctl,
|
||||
.setup = NULL,
|
||||
.setdown = NULL,
|
||||
.setaddr = NULL,
|
||||
.setdns = NULL,
|
||||
.setdhcp = NULL,
|
||||
.ping = NULL,
|
||||
.netstat = Ec801eNetstat,
|
||||
.connect = Ec801eConnect,
|
||||
.send = Ec801eSend,
|
||||
.recv = Ec801eRecv,
|
||||
.disconnect = Ec801eDisconnect,
|
||||
.mqttconnect = Ec801eMqttConnect,
|
||||
.mqttdisconnect = Ec801eMqttDisconnect,
|
||||
.mqttsend = Ec801eMqttSend,
|
||||
.mqttrecv = Ec801eMqttRecv,
|
||||
};
|
||||
|
||||
AdapterProductInfoType Ec801eAttach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("Ec801eAttach malloc product_info error\n");
|
||||
PrivFree(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(product_info->model_name, ADAPTER_4G_EC801E);
|
||||
|
||||
product_info->model_done = (void *)&ec801e_done;
|
||||
|
||||
return product_info;
|
||||
}
|
||||
|
|
@ -1,252 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ec801e_mqtt.c
|
||||
* @brief Implement the connection 4G adapter function, using EC801E device
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2025.11.22
|
||||
*/
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define EC801E_GET_QCCID_CMD "AT+QCCID\r\n"
|
||||
#define EC801E_GET_CPIN_CMD "AT+CPIN?\r\n"
|
||||
#define EC801E_GET_CREG_CMD "AT+CREG?\r\n"
|
||||
#define EC801E_CLOSE "AT+QPOWD\r\n"
|
||||
#define EC801E_SET_MQTT_MODE_CMD "AT+QMTCFG=\"recv/mode\",0,0,1\r\n"
|
||||
#define EC801E_SET_MQTT_SERVER_CMD "AT+QMTOPEN=0,"
|
||||
#define EC801E_SET_MQTT_CONNECT_CMD "AT+QMTCONN=0,"
|
||||
#define EC801E_SET_MQTT_DISCONN_CMD "AT+QMTDISC=0\r\n"
|
||||
#define EC801E_SET_MQTT_PUBEX_CMD "AT+QMTPUBEX=0,0,0,0,"
|
||||
#define EC801E_SET_MQTT_SUB_CMD "AT+QMTSUB=0,1,"
|
||||
#define EC801E_OK_REPLY "OK"
|
||||
#define EC801E_READY_REPLY "READY"
|
||||
#define EC801E_CREG_REPLY ",1"
|
||||
#define EC801E_PUBEX_REPLY ">"
|
||||
#define TRY_TIMES 10
|
||||
|
||||
int Ec801eMqttConnect(struct Adapter *adapter, const char *ip, const char *port, const char *client_id, const char *username, const char *password) {
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
uint8_t ec801e_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
PrivTaskDelay(1500); //after +++ command, wait at least 1s
|
||||
|
||||
/*step2: serial write "AT+CCID", get SIM ID*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_QCCID_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+CPIN?", check SIM status*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_CPIN_CMD, EC801E_READY_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+CREG?", check whether registered to GSM net*/
|
||||
PrivTaskDelay(1000); //before CREG command, wait 1s
|
||||
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_GET_CREG_CMD, EC801E_CREG_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step5: serial write "AT+QMTCFG=", config mqtt params*/
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_SET_MQTT_MODE_CMD, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step6: serial write "AT+OPEN=", config mqtt ip and port*/
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
strcpy(ec801e_cmd, EC801E_SET_MQTT_SERVER_CMD);
|
||||
strcat(ec801e_cmd, "\"");
|
||||
strcat(ec801e_cmd, ip);
|
||||
strcat(ec801e_cmd, "\",");
|
||||
strcat(ec801e_cmd, port);
|
||||
strcat(ec801e_cmd, "\r\n");
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step7: serial write "AT+QMTCONN=", config mqtt connection*/
|
||||
PrivTaskDelay(1000); //before mqtt connect command, wait 1s
|
||||
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
strcpy(ec801e_cmd, EC801E_SET_MQTT_CONNECT_CMD);
|
||||
strcat(ec801e_cmd, "\"");
|
||||
strcat(ec801e_cmd, client_id);
|
||||
strcat(ec801e_cmd, "\",\"");
|
||||
strcat(ec801e_cmd, username);
|
||||
strcat(ec801e_cmd, "\",\"");
|
||||
strcat(ec801e_cmd, password);
|
||||
strcat(ec801e_cmd, "\"\r\n");
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.mqttIsConnected = 1;
|
||||
|
||||
ADAPTER_DEBUG("Ec801e mqtt connect done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e mqtt connect failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Ec801eMqttDisconnect(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
PrivTaskDelay(1500); //before +++ command, wait at least 1s
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
PrivTaskDelay(1500); //after +++ command, wait at least 1s
|
||||
|
||||
/*step2: serial write "AT+QMTDISC", close mqtt connect*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_SET_MQTT_DISCONN_CMD, EC801E_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
adapter->network_info.mqttIsConnected = 0;
|
||||
|
||||
ADAPTER_DEBUG("Ec801e disconnect mqtt done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e disconnect mqtt failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Ec801eMqttSend(struct Adapter *adapter, const char *topic, const void *buf, size_t len) {
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
uint8_t ec801e_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x3E, 0x20);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
char len_str[10];
|
||||
sprintf(len_str, "%u", len);
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
strcpy(ec801e_cmd, EC801E_SET_MQTT_PUBEX_CMD);
|
||||
strcat(ec801e_cmd, "\"");
|
||||
strcat(ec801e_cmd, topic);
|
||||
strcat(ec801e_cmd, "\",");
|
||||
strcat(ec801e_cmd, len_str);
|
||||
strcat(ec801e_cmd, "\r\n");
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_PUBEX_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
EntmSend(adapter->agent, buf, len);
|
||||
|
||||
ADAPTER_DEBUG("Ec801e mqtt send done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e mqtt send failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Ec801eMqttRecv(struct Adapter *adapter, const char *topic, void *buf, size_t len) {
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
uint8_t ec801e_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
|
||||
memset(ec801e_cmd, 0, sizeof(ec801e_cmd));
|
||||
strcpy(ec801e_cmd, EC801E_SET_MQTT_SUB_CMD);
|
||||
strcat(ec801e_cmd, "\"");
|
||||
strcat(ec801e_cmd, topic);
|
||||
strcat(ec801e_cmd, "\",0\r\n");
|
||||
|
||||
PrivTaskDelay(1000); //before mqtt sub topic command, wait 1s
|
||||
for(try = 0; try < TRY_TIMES; try++){
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec801e_cmd, EC801E_OK_REPLY);
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
EntmRecv(adapter->agent, buf, len, 30);
|
||||
|
||||
ADAPTER_DEBUG("Ec200a mqtt recv done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec801e mqtt recv failed. Power down\n");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC801E_CLOSE, EC801E_OK_REPLY);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
config ADAPTER_4G_GM800TF
|
||||
string "GM800TF adapter name"
|
||||
default "gm800tf"
|
||||
|
||||
if ADD_XIZI_FEATURES
|
||||
config ADAPTER_GM800TF_DEV
|
||||
string "GM800TF device path"
|
||||
default "/dev/lte_dev1"
|
||||
endif
|
||||
|
||||
if ADD_NUTTX_FEATURES
|
||||
config ADAPTER_GM800TF_DEV
|
||||
string "GM800TF device path"
|
||||
default "/dev/ttyS8"
|
||||
endif
|
||||
|
||||
if ADD_RTTHREAD_FEATURES
|
||||
config ADAPTER_GM800TF_DEV
|
||||
string "GM800TF device path"
|
||||
default "/dev/usart8"
|
||||
endif
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
############################################################################
|
||||
# APP_Framework/Framework/connection/4g/gm800tf/Make.defs
|
||||
############################################################################
|
||||
ifneq ($(CONFIG_ADAPTER_4G_GM800TF),)
|
||||
CONFIGURED_APPS += $(APPDIR)/../../../APP_Framework/Framework/connection/4g/gm800tf
|
||||
endif
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
include $(KERNEL_ROOT)/.config
|
||||
ifeq ($(CONFIG_ADD_NUTTX_FEATURES),y)
|
||||
include $(APPDIR)/Make.defs
|
||||
CSRCS += gm800tf.c gm800tf_mqtt.c
|
||||
include $(APPDIR)/Application.mk
|
||||
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
SRC_FILES := gm800tf.c gm800tf_mqtt.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
endif
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
from building import *
|
||||
import os
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = []
|
||||
if GetDepend(['ADAPTER_GM800TF']):
|
||||
src += ['gm800tf.c']
|
||||
group = DefineGroup('connection 4g gm800tf', src, depend = [], CPPPATH = [cwd])
|
||||
|
||||
Return('group')
|
||||
|
|
@ -1,639 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file gm800tf.c
|
||||
* @brief Implement the connection 4G adapter function, using GM800TF device
|
||||
* @author Huo Yujia (huoyujia081@126.com)
|
||||
* @version 1.0
|
||||
* @date 2024-07-05
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define GM800TF_AT_MODE_CMD "+++"
|
||||
#define GM800TF_GET_ICCID_CMD "AT+ICCID\r\n"
|
||||
#define GM800TF_SETUP_SOCKET_CMD "AT+SOCK%c=TCP,%s,%s\r\n"
|
||||
#define GM800TF_OPEN_SOCKET_CMD "AT+SOCK%cEN=ON\r\n"
|
||||
#define GM800TF_CLOSE_SOCKET_CMD "AT+SOCK%cEN=OFF\r\n"
|
||||
#define GM800TF_SET_WKMOD_NET_CMD "AT+WKMOD=NET\r\n"
|
||||
#define GM800TF_GET_WKMOD_CMD "AT+WKMOD?\r\n"
|
||||
#define GM800TF_SAVE_CFG_CMD "AT+S\r\n"
|
||||
#define GM800TF_GET_CSQ_CMD "AT+CSQ\r\n"
|
||||
#define GM800TF_ENTM_MODE_CMD "AT+ENTM\r\n"
|
||||
#define GM800TF_GET_SOCKET_PARAM_CMD "AT+SOCK%c?\r\n"
|
||||
#define GM800TF_GET_SOCKET_STATUS_CMD "AT+SOCK%cLK\r\n"
|
||||
#define GM800TF_RESET_CMD "AT+CLEAR\r\n"
|
||||
#define GM800TF_SET_HEART_CMD "AT+HEARTEN=%s\r\n"
|
||||
#define GM800TF_SET_HEART_DATA_CMD "AT+HEARTDT=%s\r\n"
|
||||
#define GM800TF_SET_HEART_TIME_CMD "AT+HEARTTM=%d\r\n"
|
||||
#define GM800TF_GET_MQTT_STATUS_CMD "AT+MQTTSTA?\r\n"
|
||||
#define GM800TF_GET_MQTT_SERVER_CMD "AT+MQTTSVR?\r\n"
|
||||
#define GM800TF_GET_MQTT_CLIENTID_CMD "AT+MQTTCID?\r\n"
|
||||
#define GM800TF_GET_MQTT_USERNAME_CMD "AT+MQTTUSER?\r\n"
|
||||
#define GM800TF_GET_MQTT_PASSWORD_CMD "AT+MQTTPSW?\r\n"
|
||||
|
||||
#define GM800TF_OK_REPLY "OK"
|
||||
#define GM800TF_READY_REPLY "READY"
|
||||
#define GM800TF_CREG_REPLY ",1"
|
||||
#define GM800TF_CONNECT_REPLY "CONNECT"
|
||||
|
||||
#define TRY_TIMES 10
|
||||
|
||||
/**
|
||||
* @brief convert string to corresponding hex string
|
||||
* @param input original string
|
||||
* @param output hex string
|
||||
*/
|
||||
void string_to_hex(const char *input, char *output) {
|
||||
while (*input) {
|
||||
sprintf(output, "%02X", (unsigned char)*input);
|
||||
input++;
|
||||
output += 2;
|
||||
}
|
||||
*output = '\0'; // Null-terminate the output string
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief enter the configuration mode
|
||||
* @param adapter
|
||||
* @return int
|
||||
*/
|
||||
int Gm800tfEnterAtMode(struct Adapter *adapter) {
|
||||
AtSetReplyEndChar(adapter->agent, 'o', 'k');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 1);
|
||||
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
PrivTaskDelay(500);
|
||||
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "a");
|
||||
PrivTaskDelay(100);
|
||||
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "\r\n"); // clear the "+++a", confirm it will not be added to the following command.
|
||||
PrivTaskDelay(500);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief open the GM800TF(aka. init the serial) and init the AT agent
|
||||
* @param adapter
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfOpen(struct Adapter *adapter) {
|
||||
/*step1: open gm800tf serial port*/
|
||||
adapter->fd = PrivOpen(ADAPTER_GM800TF_DEV, O_RDWR);
|
||||
if (adapter->fd < 0) {
|
||||
printf("Gm800tfOpen get serial %s fd error\n", ADAPTER_GM800TF_DEV);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*step2: init AT agent*/
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "4G_uart_client";
|
||||
if (0 != InitATAgent(agent_name, adapter->fd, 256)) {
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
|
||||
adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
/* step3: reset gm800tf */
|
||||
ADAPTER_DEBUG("Gm800tf reseting configuration\n");
|
||||
Gm800tfEnterAtMode(adapter);
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+CLEAR\r\n");
|
||||
PrivTaskDelay(15000); // after reset configuration, wait at least 10s for restarting
|
||||
|
||||
/* step4: communication from sockA channel only*/
|
||||
adapter->socket.socket_id = 0;
|
||||
|
||||
ADAPTER_DEBUG("Gm800tf open done\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief close the GM800TF(aka. disable the interrupt) and disconnect the socket
|
||||
* @param adapter
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfClose(struct Adapter *adapter) {
|
||||
int ret = 0;
|
||||
uint8_t gm800tf_cmd[64];
|
||||
|
||||
if (!adapter->agent) {
|
||||
printf("Gm800tfClose AT agent NULL\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); //'O', 'K'
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step2: serial write "AT+SOCKAEN=OFF" or "AT+SOCKBEN=OFF", close socket connect before open socket*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_CLOSE_SOCKET_CMD, 'A' + adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+S", save configuration and restart 4G module*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SAVE_CFG_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(15000); // after save configuration, wait at least 10s for restarting
|
||||
|
||||
out:
|
||||
/*step4: close gm800tf serial port and delete ATAgentReceiveProcess*/
|
||||
// DeleteATAgent(adapter->agent);
|
||||
// adapter->agent = NULL;
|
||||
PrivClose(adapter->fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief config the serial and enable the interrupt
|
||||
* @param adapter
|
||||
* @param cmd
|
||||
* @param args
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfIoctl(struct Adapter *adapter, int cmd, void *args) {
|
||||
if (OPE_INT != cmd) {
|
||||
printf("Gm800tfIoctl only support OPE_INT, do not support %d\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t baud_rate = *((uint32_t *)args);
|
||||
|
||||
struct SerialDataCfg serial_cfg;
|
||||
memset(&serial_cfg, 0, sizeof(struct SerialDataCfg));
|
||||
serial_cfg.serial_baud_rate = baud_rate;
|
||||
serial_cfg.serial_data_bits = DATA_BITS_8;
|
||||
serial_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
serial_cfg.serial_parity_mode = PARITY_NONE;
|
||||
serial_cfg.serial_bit_order = STOP_BITS_1;
|
||||
serial_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
#ifdef TOOL_USING_OTA
|
||||
serial_cfg.serial_timeout = OTA_RX_TIMEOUT;
|
||||
#else
|
||||
// serial receive timeout 10s
|
||||
serial_cfg.serial_timeout = 100000;
|
||||
#endif
|
||||
serial_cfg.is_ext_uart = 0;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = &serial_cfg;
|
||||
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief connect to the server
|
||||
* @param adapter
|
||||
* @param net_role
|
||||
* @param ip
|
||||
* @param port
|
||||
* @param ip_type
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type) {
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
uint8_t gm800tf_cmd[64];
|
||||
|
||||
/*step0: compare ip and port with current configuration, and if they are the same there is no need to connect again*/
|
||||
AdapterDeviceNetstat(adapter);
|
||||
if (adapter->network_info.workMode == 1 && strcmp(adapter->network_info.ip_address, ip) == 0 &&
|
||||
adapter->network_info.port == atoi(port) && adapter->network_info.is_connected && adapter->network_info.signal_strength < 99) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*step1: serial write "+++" and "a", quit transparent mode*/
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
|
||||
/*step2: serial write "AT+ICCID", get SIM ID*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_GET_ICCID_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+SOCKA=<protocol>,<address>,<port>" or "AT+SOCKB=<protocol>,<address>,<port>", connect
|
||||
* socket using TCP*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SETUP_SOCKET_CMD, 'A' + adapter->socket.socket_id, ip, port);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+SOCKAEN=ON" or "AT+SOCKBEN=ON", connect socket using TCP*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_OPEN_SOCKET_CMD, 'A' + adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step5: set up the heartbeat */
|
||||
// char *heartbeatData = "heartbeat test";
|
||||
// int heartbeatTime = 30;
|
||||
// char heartbeatDataHex[65] = {}; // 最多32个字符
|
||||
// string_to_hex(heartbeatData, heartbeatDataHex);
|
||||
// memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
// sprintf(gm800tf_cmd, GM800TF_SET_HEART_CMD, "ON");
|
||||
// ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
// if (ret < 0) {
|
||||
// goto out;
|
||||
// }
|
||||
// memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
// sprintf(gm800tf_cmd, GM800TF_SET_HEART_DATA_CMD, heartbeatDataHex);
|
||||
// ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
// if (ret < 0) {
|
||||
// goto out;
|
||||
// }
|
||||
// memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
// sprintf(gm800tf_cmd, GM800TF_SET_HEART_TIME_CMD, heartbeatTime);
|
||||
// ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
// if (ret < 0) {
|
||||
// goto out;
|
||||
// }
|
||||
|
||||
/* step5: set down the heartbeat */
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_HEART_CMD, "OFF");
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step6: serial write "AT+WKMOD=NET", set the working mode to network transparent transmission mode */
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SET_WKMOD_NET_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step7: serial write "AT+S", save configuration and restart 4G module*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SAVE_CFG_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(15000); // after save configuration, wait at least 10s for restarting
|
||||
ADAPTER_DEBUG("Gm800tf connect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Gm800tf connect TCP failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief send the data to the server after connect to the server
|
||||
* @param adapter
|
||||
* @param buf
|
||||
* @param len
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfSend(struct Adapter *adapter, const void *buf, size_t len) {
|
||||
/* query for whether socket is connected successfully */
|
||||
AdapterDeviceNetstat(adapter);
|
||||
if (adapter->network_info.is_connected == 0) {
|
||||
printf("[error %s %d]socket has not been connected, please call function AdapterDeviceConnect\n", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* send the buf data */
|
||||
if (adapter->agent) {
|
||||
EntmSend(adapter->agent, (const char *)buf, len);
|
||||
} else {
|
||||
printf("Gm800tfSend can not find agent\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief receive the data from the server after connect to the server
|
||||
* @param adapter
|
||||
* @param buf
|
||||
* @param len
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfRecv(struct Adapter *adapter, void *buf, size_t len) {
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 6);
|
||||
} else {
|
||||
printf("Gm800tfRecv can not find agent\n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief disconnect the server
|
||||
* @param adapter
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfDisconnect(struct Adapter *adapter) {
|
||||
int ret = 0;
|
||||
uint8_t gm800tf_cmd[64];
|
||||
|
||||
/* query for whether socket is connected successfully */
|
||||
AdapterDeviceNetstat(adapter);
|
||||
if (adapter->network_info.is_connected == 0) {
|
||||
printf("[error %s %d]socket has not been connected, please call function AdapterDeviceConnect\n", __func__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
|
||||
/*step2: serial write "AT+SOCKAEN=OFF" or "AT+SOCKBEN=OFF", close socket connect before open socket*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_CLOSE_SOCKET_CMD, 'A' + adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+S", save configuration and restart 4G module*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SAVE_CFG_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(15000); // after save configuration, wait at least 5s for restarting
|
||||
ADAPTER_DEBUG("Gm800tf disconnect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Gm800tf disconnect TCP failed. \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief query for the network status
|
||||
* @param adapter
|
||||
* @return int
|
||||
*/
|
||||
static int Gm800tfNetstat(struct Adapter *adapter) {
|
||||
char result[96] = {0};
|
||||
uint8_t gm800tf_cmd[64];
|
||||
|
||||
int ret = 0;
|
||||
int try = 0;
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
|
||||
/*step2: serial write "AT+ICCID", get SIM ID*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_GET_ICCID_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
printf("[error %s %d]AT+ICCID failed, please check the SIM card\n", __func__, __LINE__);
|
||||
goto out;
|
||||
}
|
||||
|
||||
printf("-*-*-*-*-*-*-*Gm800tfNetstat*-*-*-*-*-*-*\n");
|
||||
/*step3: serial write "AT+CSQ", get carrier type*/
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_CSQ_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(strstr(result, "+CSQ:"), "+CSQ: %d", &adapter->network_info.signal_strength) == 1) {
|
||||
printf("signal strength:\t%d\n", adapter->network_info.signal_strength);
|
||||
} else {
|
||||
printf("Failed to parse signal strength\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+WKMOD?", get the current work mode*/
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_WKMOD_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
char workMode[10] = {};
|
||||
if (sscanf(strstr(result, "+WKMOD:"), "+WKMOD:%s", workMode) == 1) {
|
||||
printf("work mode:\t\t%s\n", workMode);
|
||||
if (strcmp(workMode, "NET") == 0) {
|
||||
adapter->network_info.workMode = 1;
|
||||
} else if (strcmp(workMode, "MQTT,NOR") == 0) {
|
||||
adapter->network_info.workMode = 2;
|
||||
} else {
|
||||
adapter->network_info.workMode = 0;
|
||||
}
|
||||
} else {
|
||||
printf("Failed to parse work mode\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step5: serial write "AT+SOCKA=?", get the current server IP address*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_GET_SOCKET_PARAM_CMD, 'A' + adapter->socket.socket_id);
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, gm800tf_cmd, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(strstr(result, ",") + 1, "%15[^,],%hu", adapter->network_info.ip_address, &adapter->network_info.port) == 2) {
|
||||
printf("server ip address:\t%s\n", adapter->network_info.ip_address);
|
||||
printf("server port:\t\t%hu\n", adapter->network_info.port);
|
||||
} else {
|
||||
printf("server ip address:\t%s\n", adapter->network_info.ip_address);
|
||||
printf("server port:\t\t%d\n", adapter->network_info.port);
|
||||
printf("Failed to parse IP address and port\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step6: serial write "AT+SOCKALK" or "AT+SOCKBLK", get socket status*/
|
||||
memset(result, 0, sizeof(result));
|
||||
sprintf(gm800tf_cmd, GM800TF_GET_SOCKET_STATUS_CMD, 'A' + adapter->socket.socket_id);
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, gm800tf_cmd, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
char socket_status[20] = {};
|
||||
if (sscanf(strstr(result, "LK:"), "LK: %s", socket_status) == 1) {
|
||||
printf("socket status:\t\t%s\n", socket_status);
|
||||
adapter->network_info.is_connected = strcmp(socket_status, "Connected") == 0 ? 1 : 0;
|
||||
} else {
|
||||
printf("Failed to parse socket status\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step7: serial write "AT+MQTTSVR=?", get the current mqtt server IP address and port*/
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_MQTT_SERVER_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(strstr(result, ":") + 1, "%15[^,],%hu", adapter->network_info.mqttServerIp, &adapter->network_info.mqttServerPort) == 2) {
|
||||
printf("mqtt server ip address:\t%s\n", adapter->network_info.mqttServerIp);
|
||||
printf("mqtt server port:\t%hu\n", adapter->network_info.mqttServerPort);
|
||||
} else {
|
||||
printf("mqtt server ip address:\t%s\n", adapter->network_info.mqttServerIp);
|
||||
printf("mqtt server port:\t%d\n", adapter->network_info.mqttServerPort);
|
||||
printf("Failed to parse IP address and port\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step8: serial write "AT+MQTTCID=?", get the current mqtt client id*/
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_MQTT_CLIENTID_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(strstr(result, "MQTTCID:"), "MQTTCID:%s", adapter->network_info.mqttClientId) == 1) {
|
||||
printf("mqtt clientid:\t\t%s\n", adapter->network_info.mqttClientId);
|
||||
} else {
|
||||
printf("Failed to parse mqtt clientid\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step9: serial write "AT+MQTTUSER=?", get the current mqtt username*/
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_MQTT_USERNAME_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(strstr(result, "MQTTUSER:"), "MQTTUSER:%s", adapter->network_info.mqttUsername) == 1) {
|
||||
printf("mqtt username:\t\t%s\n", adapter->network_info.mqttUsername);
|
||||
} else {
|
||||
printf("Failed to parse mqtt username\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step10: serial write "AT+MQTTPSW?", get the current mqtt password */
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_MQTT_PASSWORD_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
if (sscanf(strstr(result, "MQTTPSW:"), "MQTTPSW:%s", adapter->network_info.mqttPassword) == 1) {
|
||||
printf("mqtt password:\t\t%s\n", adapter->network_info.mqttPassword);
|
||||
} else {
|
||||
printf("Failed to parse mqtt password\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step11: serial write "AT+MQTTSTA", get the status of mqtt connection*/
|
||||
memset(result, 0, sizeof(result));
|
||||
ret = AtGetNetworkInfoReply(adapter->agent, GM800TF_GET_MQTT_STATUS_CMD, result);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
char mqtt_status[20] = {};
|
||||
if (sscanf(strstr(result, "STA:"), "STA: %s", mqtt_status) == 1) {
|
||||
printf("mqtt status:\t\t%s\n", mqtt_status);
|
||||
adapter->network_info.mqttIsConnected = strcmp(mqtt_status, "\"CONNECTION\"") == 0 ? 1 : 0;
|
||||
} else {
|
||||
printf("Failed to parse mqtt status\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step12: serial write "AT+ENTM", enter entm mode */
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_ENTM_MODE_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n");
|
||||
return 0;
|
||||
|
||||
out:
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n");
|
||||
ADAPTER_DEBUG("Gm800tf get netstat failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Gm800tfMqttConnect(struct Adapter *adapter, const char *ip, const char *port, const char *client_id, const char *username,
|
||||
const char *password);
|
||||
int Gm800tfMqttDisconnect(struct Adapter *adapter);
|
||||
int Gm800tfMqttSend(struct Adapter *adapter, const char *topic, const void *buf, size_t len);
|
||||
int Gm800tfMqttRecv(struct Adapter *adapter, const char *topic, void *buf, size_t len);
|
||||
|
||||
static const struct IpProtocolDone gm800tf_done = {
|
||||
.open = Gm800tfOpen,
|
||||
.close = Gm800tfClose,
|
||||
.ioctl = Gm800tfIoctl,
|
||||
.setup = NULL,
|
||||
.setdown = NULL,
|
||||
.setaddr = NULL,
|
||||
.setdns = NULL,
|
||||
.setdhcp = NULL,
|
||||
.ping = NULL,
|
||||
.netstat = Gm800tfNetstat,
|
||||
.connect = Gm800tfConnect,
|
||||
.send = Gm800tfSend,
|
||||
.recv = Gm800tfRecv,
|
||||
.disconnect = Gm800tfDisconnect,
|
||||
.mqttconnect = Gm800tfMqttConnect,
|
||||
.mqttdisconnect = Gm800tfMqttDisconnect,
|
||||
.mqttsend = Gm800tfMqttSend,
|
||||
.mqttrecv = Gm800tfMqttRecv,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief expose the behavior of GM800TF to application
|
||||
* @param adapter
|
||||
* @return AdapterProductInfoType
|
||||
*/
|
||||
AdapterProductInfoType Gm800tfAttach(struct Adapter *adapter) {
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("Gm800tfAttach malloc product_info error\n");
|
||||
PrivFree(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(product_info->model_name, ADAPTER_4G_GM800TF);
|
||||
|
||||
product_info->model_done = (void *)&gm800tf_done;
|
||||
|
||||
return product_info;
|
||||
}
|
||||
|
|
@ -1,316 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file gm800tf_mqtt.c
|
||||
* @brief Implement the connection 4G adapter function about mqtt, using GM800TF device
|
||||
* @author Huo Yujia (huoyujia081@126.com)
|
||||
* @version 1.0
|
||||
* @date 2024-07-26
|
||||
*/
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define GM800TF_GET_ICCID_CMD "AT+ICCID\r\n"
|
||||
#define GM800TF_SET_WKMOD_MQTT_CMD "AT+WKMOD=MQTT,NOR\r\n"
|
||||
#define GM800TF_SET_WKMOD_CMD_CMD "AT+WKMOD=CMD\r\n"
|
||||
#define GM800TF_GET_MQTT_PUBPARAM_CMD "AT+MQTTPUBTP?\r\n"
|
||||
#define GM800TF_SET_MQTT_PUBPARAM_CMD "AT+MQTTPUBTP=%d,%d,%s,%d,%d\r\n"
|
||||
#define GM800TF_SET_MQTT_SUBPARAM_CMD "AT+MQTTSUBTP=%d,%d,%s,%d\r\n"
|
||||
#define GM800TF_SET_MQTT_SERVER_CMD "AT+MQTTSVR=%s,%s\r\n"
|
||||
#define GM800TF_SET_MQTT_USERNAME_CMD "AT+MQTTUSER=%s\r\n"
|
||||
#define GM800TF_SET_MQTT_PASSWORD_CMD "AT+MQTTPSW=%s\r\n"
|
||||
#define GM800TF_SET_MQTT_CLIID_CMD "AT+MQTTCID=%s\r\n"
|
||||
#define GM800TF_SET_MQTT_MODE_CMD "AT+MQTTMOD=%d\r\n"
|
||||
#define GM800TF_SET_HEART_DATA_CMD "AT+HEARTDT=%s\r\n"
|
||||
#define GM800TF_SET_HEART_TIME_CMD "AT+HEARTTM=%d\r\n"
|
||||
#define GM800TF_SAVE_CFG_CMD "AT+S\r\n"
|
||||
#define GM800TF_ENTM_MODE_CMD "AT+ENTM\r\n"
|
||||
|
||||
#define GM800TF_OK_REPLY "OK"
|
||||
#define GM800TF_READY_REPLY "READY"
|
||||
#define GM800TF_CREG_REPLY ",1"
|
||||
#define GM800TF_PUBEX_REPLY ">"
|
||||
|
||||
#define TRY_TIMES 10
|
||||
|
||||
int Gm800tfEnterAtMode(struct Adapter *adapter); // defined in gm800tf.c
|
||||
|
||||
/**
|
||||
* @brief connect to mqtt server
|
||||
* @param adapter
|
||||
* @param ip mqtt server ip address
|
||||
* @param port mqtt server port
|
||||
* @param client_id mqtt client id
|
||||
* @param username mqtt username
|
||||
* @param password mqtt password
|
||||
* @return int 0: success, -1: failed
|
||||
*/
|
||||
int Gm800tfMqttConnect(struct Adapter *adapter, const char *ip, const char *port, const char *client_id, const char *username,
|
||||
const char *password) {
|
||||
int ret = 0;
|
||||
uint8_t gm800tf_cmd[64];
|
||||
|
||||
/*step0: compare ip and port with current configuration, and if they are the same there is no need to connect again*/
|
||||
AdapterDeviceNetstat(adapter);
|
||||
if (adapter->network_info.workMode == 2 && strcmp(adapter->network_info.mqttServerIp, ip) == 0 &&
|
||||
adapter->network_info.mqttServerPort == atoi(port) && strcmp(adapter->network_info.mqttClientId, client_id) == 0 &&
|
||||
strcmp(adapter->network_info.mqttUsername, username) == 0 && strcmp(adapter->network_info.mqttPassword, password) == 0 &&
|
||||
adapter->network_info.mqttIsConnected && adapter->network_info.signal_strength < 99) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*step1: serial write "+++" and "a", quit transparent mode*/
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
|
||||
/*step2: serial write "AT+ICCID", get SIM ID*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_GET_ICCID_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+MQTTSVR=<server>,<port>", config mqtt ip and port*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_SERVER_CMD, ip, port);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+MQTTCID=<clientid>", config mqtt client id*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_CLIID_CMD, client_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step5: serial write "AT+MQTTUSER=<username>", config mqtt username*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_USERNAME_CMD, username);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step6: serial write "AT+MQTTPSW=<password>", config mqtt password*/
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_PASSWORD_CMD, password);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step7: serial write "AT+WKMOD=MQTT", set the working mode to mqtt mode*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SET_WKMOD_MQTT_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step8: serial write "AT+MQTTMOD=<mode>, set the mqtt mode*/
|
||||
/* mode-0: Send data to all preset enabled topics */
|
||||
/* mode-1: Send data separately to a specific topic */
|
||||
int mode = 1;
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_MODE_CMD, mode);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step9: set the heartbeat parameter*/
|
||||
char *heartbeatData = "heartbeat test";
|
||||
int heartbeatTime = 30;
|
||||
char heartbeatDataHex[65] = {}; // 最多32个字符
|
||||
void string_to_hex(const char *input, char *output);
|
||||
string_to_hex(heartbeatData, heartbeatDataHex);
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_HEART_DATA_CMD, heartbeatDataHex);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_HEART_TIME_CMD, heartbeatTime);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step10: serial write "AT+S", save configuration and restart mqtt module*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SAVE_CFG_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(20000); // after save configuration, wait at least 10s for restarting
|
||||
ADAPTER_DEBUG("Gm800tf mqtt connect done\n");
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Gm800tf mqtt connect failed. Power down\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief this function has no use
|
||||
* @param adapter
|
||||
* @return int
|
||||
*/
|
||||
int Gm800tfMqttDisconnect(struct Adapter *adapter) {
|
||||
int ret = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief send data to mqtt server
|
||||
* @param adapter
|
||||
* @param topic mqtt publish topic
|
||||
* @param buf data buffer to send to mqtt server
|
||||
* @param len data length
|
||||
* @return int 0: success; others: failed
|
||||
*/
|
||||
int Gm800tfMqttSend(struct Adapter *adapter, const char *topic, const void *buf, size_t len) {
|
||||
/* query for whether socket is connected successfully */
|
||||
AdapterDeviceNetstat(adapter);
|
||||
if (adapter->network_info.mqttIsConnected == 0) {
|
||||
printf("[error %s %d]socket has not been connected, please call function AdapterDeviceMqttConnect\n", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
char result[256] = {};
|
||||
uint8_t gm800tf_cmd[64];
|
||||
|
||||
static const char *publishTopics[10] = {NULL}; // save the mqtt publish topics temporarily
|
||||
static int topicIndex = 0; // the index next mqtt topic to be saved
|
||||
|
||||
/* step0: check if the topic in the topics array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (publishTopics[i] != NULL && strcmp(publishTopics[i], topic) == 0) {
|
||||
char num[10];
|
||||
sprintf(num, "%d,", i + 1);
|
||||
EntmSend(adapter->agent, num, strlen(num));
|
||||
EntmSend(adapter->agent, (const char *)buf, len);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
topicIndex = topicIndex >= 10 ? 0 : topicIndex;
|
||||
publishTopics[topicIndex] = topic; // save the topic to the topics array
|
||||
|
||||
/* step1: serial send "+++" and "a", quit transparent mode */
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
|
||||
/* step2: serial write "AT+MQTTPUBTP=<pubnum>,<puben>,<topic>,<qos>,<retained>", config the publish parameters */
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_PUBPARAM_CMD, topicIndex + 1, 1, topic, 2, 0);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* step3: serial write "AT+S", save the configuration and restart the module */
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SAVE_CFG_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(15000); // after save configuration, wait at least 10s for restarting
|
||||
|
||||
/* step4: send the data to mqtt server */
|
||||
char num[10];
|
||||
sprintf(num, "%d,", topicIndex + 1);
|
||||
EntmSend(adapter->agent, num, strlen(num));
|
||||
EntmSend(adapter->agent, buf, len);
|
||||
topicIndex++;
|
||||
|
||||
ADAPTER_DEBUG("Gm800tf mqtt send done\n");
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Gm800tf mqtt send failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GM800TF only support transparent mode, so the topic is not used. The Message can't be diffrentiated by
|
||||
* topic in the received data.
|
||||
* @param adapter
|
||||
* @param topic
|
||||
* @param buf
|
||||
* @param len
|
||||
* @return int
|
||||
* @note 该函数暂时未经过测试
|
||||
*/
|
||||
int Gm800tfMqttRecv(struct Adapter *adapter, const char *topic, void *buf, size_t len) {
|
||||
int ret = 0;
|
||||
static const char *subscribeTopics[10] = {NULL};
|
||||
static int topicIndex = 0;
|
||||
uint8_t gm800tf_cmd[64];
|
||||
/* check if the topic in the topics array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (subscribeTopics[i] != NULL && strcmp(subscribeTopics[i], topic) == 0) {
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 6);
|
||||
} else {
|
||||
printf("Gm800tfRecv can not find agent\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
topicIndex = topicIndex >= 10 ? 0 : topicIndex;
|
||||
subscribeTopics[topicIndex] = topic;
|
||||
|
||||
ret = Gm800tfEnterAtMode(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 'O', 'K');
|
||||
AtSetReplyCharNum(adapter->agent, 256);
|
||||
AtSetReplyLrEnd(adapter->agent, 0);
|
||||
|
||||
memset(gm800tf_cmd, 0, sizeof(gm800tf_cmd));
|
||||
sprintf(gm800tf_cmd, GM800TF_SET_MQTT_SUBPARAM_CMD, topicIndex + 1, 1, topic, 2);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, gm800tf_cmd, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, GM800TF_SAVE_CFG_CMD, GM800TF_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
PrivTaskDelay(15000); // after save configuration, wait at least 10s for restarting
|
||||
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 6);
|
||||
} else {
|
||||
printf("Gm800tfRecv can not find agent\n");
|
||||
}
|
||||
out:
|
||||
ADAPTER_DEBUG("Gm800tf mqtt receive failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -129,18 +129,6 @@ struct NetworkInfo {
|
|||
enum CarrierType carrier_type;
|
||||
int signal_strength;
|
||||
char ip_address[16];
|
||||
unsigned char is_connected; // SOCKET连接状态,0为失败,1为成功
|
||||
unsigned char mqttIsConnected; // MQTT连接状态,0为失败,1为成功
|
||||
#ifdef ADAPTER_GM800TF
|
||||
unsigned char workMode; // 工作模式,1为NET,2为MQTT,NOR
|
||||
unsigned short port; // 目的端口号
|
||||
unsigned char mqttSwitch; // MQTT开关
|
||||
char mqttServerIp[16]; // MQTT目的IP地址
|
||||
unsigned short mqttServerPort; // MQTT目的端口号
|
||||
char mqttClientId[64]; // MQTT客户端ID
|
||||
char mqttUsername[64]; // MQTT用户名
|
||||
char mqttPassword[64]; // MQTT密码
|
||||
#endif
|
||||
};
|
||||
|
||||
struct AdapterData
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file xs_adapterAT_client.c
|
||||
|
|
@ -18,6 +18,7 @@
|
|||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
|
||||
#include <at_agent.h>
|
||||
#include <adapter.h>
|
||||
#include <stdbool.h>
|
||||
|
|
@ -26,7 +27,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
#include <user_api.h>
|
||||
# include <user_api.h>
|
||||
#endif
|
||||
#ifdef ADD_RTTHREAD_FEATURES
|
||||
#include <rtthread.h>
|
||||
|
|
@ -48,11 +49,9 @@ unsigned int IpTint(char *ipstr)
|
|||
|
||||
token = strtok(ipstr, ".");
|
||||
|
||||
while (token != NULL)
|
||||
{
|
||||
while (token != NULL) {
|
||||
cur = atoi(token);
|
||||
if (cur >= 0 && cur <= 255)
|
||||
{
|
||||
if (cur >= 0 && cur <= 255) {
|
||||
total += cur * pow(256, i);
|
||||
}
|
||||
i--;
|
||||
|
|
@ -66,10 +65,8 @@ void SwapStr(char *str, int begin, int end)
|
|||
{
|
||||
int i, j;
|
||||
|
||||
for (i = begin, j = end; i <= j; i++, j--)
|
||||
{
|
||||
if (str[i] != str[j])
|
||||
{
|
||||
for (i = begin, j = end; i <= j; i++, j--) {
|
||||
if (str[i] != str[j]) {
|
||||
str[i] = str[i] ^ str[j];
|
||||
str[j] = str[i] ^ str[j];
|
||||
str[i] = str[i] ^ str[j];
|
||||
|
|
@ -86,8 +83,7 @@ char *IpTstr(unsigned int ipint)
|
|||
char token[4];
|
||||
int bt, ed, len, cur;
|
||||
|
||||
while (ipint)
|
||||
{
|
||||
while (ipint) {
|
||||
cur = ipint % 256;
|
||||
sprintf(token, "%d", cur);
|
||||
strcat(new, token);
|
||||
|
|
@ -99,10 +95,8 @@ char *IpTstr(unsigned int ipint)
|
|||
len = strlen(new);
|
||||
SwapStr(new, 0, len - 1);
|
||||
|
||||
for (bt = ed = 0; ed < len;)
|
||||
{
|
||||
while (ed < len && new[ed] != '.')
|
||||
{
|
||||
for (bt = ed = 0; ed < len;) {
|
||||
while (ed < len && new[ed] != '.') {
|
||||
ed++;
|
||||
}
|
||||
SwapStr(new, bt, ed - 1);
|
||||
|
|
@ -131,15 +125,14 @@ void ATSprintf(int fd, const char *format, va_list params)
|
|||
{
|
||||
last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, params);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("AT send %s len %u\n", send_buf, last_cmd_len);
|
||||
printf("AT send %s len %u\n",send_buf, last_cmd_len);
|
||||
#endif
|
||||
PrivWrite(fd, send_buf, last_cmd_len);
|
||||
PrivWrite(fd, send_buf, last_cmd_len);
|
||||
}
|
||||
|
||||
int ATOrderSend(ATAgentType agent, uint32_t timeout_s, ATReplyType reply, const char *cmd_expr, ...)
|
||||
{
|
||||
if (agent == NULL)
|
||||
{
|
||||
if (agent == NULL) {
|
||||
printf("ATAgent is null");
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -165,23 +158,19 @@ int ATOrderSend(ATAgentType agent, uint32_t timeout_s, ATReplyType reply, const
|
|||
agent->reply = reply;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
if (agent->reply != NULL)
|
||||
{
|
||||
if(agent->reply != NULL) {
|
||||
PrivMutexObtain(&agent->lock);
|
||||
reply->reply_len = 0;
|
||||
va_start(params, cmd_expr);
|
||||
ATSprintf(agent->fd, cmd_expr, params);
|
||||
va_end(params);
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != 0)
|
||||
{
|
||||
printf("take sem %d timeout\n", agent->rsp_sem);
|
||||
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != 0) {
|
||||
printf("take sem %d timeout\n",agent->rsp_sem);
|
||||
result = -2;
|
||||
goto __out;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
PrivMutexObtain(&agent->lock);
|
||||
va_start(params, cmd_expr);
|
||||
ATSprintf(agent->fd, cmd_expr, params);
|
||||
|
|
@ -198,41 +187,35 @@ int AtCmdConfigAndCheck(ATAgentType agent, char *cmd, char *check)
|
|||
{
|
||||
int ret = 0;
|
||||
char *result = NULL;
|
||||
if (NULL == agent || NULL == cmd || NULL == check)
|
||||
{
|
||||
if (NULL == agent || NULL == cmd || NULL == check ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ATReplyType reply = CreateATReply(256);
|
||||
if (NULL == reply)
|
||||
{
|
||||
printf("%s %d at_create_resp failed!\n", __func__, __LINE__);
|
||||
if (NULL == reply) {
|
||||
printf("%s %d at_create_resp failed!\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ret = ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd);
|
||||
if(ret < 0){
|
||||
printf("%s %d ATOrderSend failed, cmd: %s.\n",__func__,__LINE__,cmd);
|
||||
printf("%s %d ATOrderSend failed.\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
// PrivTaskDelay(3000);
|
||||
//PrivTaskDelay(3000);
|
||||
|
||||
result = GetReplyText(reply);
|
||||
if (!result)
|
||||
{
|
||||
printf("%s %n get reply failed.\n", __func__, __LINE__);
|
||||
if (!result) {
|
||||
printf("%s %n get reply failed.\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("[reply result: %s]\n", result);
|
||||
#endif
|
||||
if (!strstr(result, check))
|
||||
{
|
||||
printf("%s %d cmd[%s] check[%s] reply[%s] failed.\n", __func__, __LINE__, cmd, check, result);
|
||||
if(!strstr(result, check)) {
|
||||
printf("%s %d check[%s] reply[%s] failed.\n",__func__,__LINE__,check,result);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
|
@ -245,40 +228,34 @@ __exit:
|
|||
int AtGetNetworkInfoReply(ATAgentType agent, char *cmd, char *result)
|
||||
{
|
||||
int ret = 0;
|
||||
if (NULL == agent || NULL == cmd)
|
||||
{
|
||||
if (NULL == agent || NULL == cmd) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ATReplyType reply = CreateATReply(256);
|
||||
if (NULL == reply)
|
||||
{
|
||||
printf("%s %d at_create_resp failed!\n", __func__, __LINE__);
|
||||
if (NULL == reply) {
|
||||
printf("%s %d at_create_resp failed!\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ret = ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("%s %d ATOrderSend failed.\n", __func__, __LINE__);
|
||||
if(ret < 0){
|
||||
printf("%s %d ATOrderSend failed.\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
const char *replyText = GetReplyText(reply);
|
||||
if (replyText == NULL || replyText[0] == '\0')
|
||||
{
|
||||
printf("%s %n get reply failed.\n", __func__, __LINE__);
|
||||
if (replyText == NULL || replyText[0] == '\0') {
|
||||
printf("%s %n get reply failed.\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
strncpy(result, replyText, 63);
|
||||
result[63] = '\0';
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("[reply result: %s]\n", result);
|
||||
#endif
|
||||
|
||||
__exit:
|
||||
DeleteATReply(reply);
|
||||
|
|
@ -292,8 +269,7 @@ char *GetReplyText(ATReplyType reply)
|
|||
|
||||
int AtSetReplyLrEnd(ATAgentType agent, char enable)
|
||||
{
|
||||
if (!agent)
|
||||
{
|
||||
if (!agent) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -304,8 +280,7 @@ int AtSetReplyLrEnd(ATAgentType agent, char enable)
|
|||
|
||||
int AtSetReplyEndChar(ATAgentType agent, char last_ch, char end_ch)
|
||||
{
|
||||
if (!agent)
|
||||
{
|
||||
if (!agent) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -317,8 +292,7 @@ int AtSetReplyEndChar(ATAgentType agent, char last_ch, char end_ch)
|
|||
|
||||
int AtSetReplyCharNum(ATAgentType agent, unsigned int num)
|
||||
{
|
||||
if (!agent)
|
||||
{
|
||||
if (!agent) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -329,9 +303,8 @@ int AtSetReplyCharNum(ATAgentType agent, unsigned int num)
|
|||
|
||||
int EntmSend(ATAgentType agent, const char *data, int len)
|
||||
{
|
||||
if (len > 256)
|
||||
{
|
||||
printf("send length %d more then max 256 Bytes.\n", len);
|
||||
if(len > 256){
|
||||
printf("send length %d more then max 256 Bytes.\n",len);
|
||||
return -1;
|
||||
}
|
||||
char *send_buff = (char *)PrivMalloc(256);
|
||||
|
|
@ -360,20 +333,18 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
|
|||
uint32 real_recv_len = 0;
|
||||
|
||||
abstime.tv_sec = timeout_s;
|
||||
if (buffer_len > ENTM_RECV_MAX)
|
||||
{
|
||||
printf("read length more then max length[%d] Bytes", ENTM_RECV_MAX);
|
||||
return -1;
|
||||
if(buffer_len > ENTM_RECV_MAX){
|
||||
printf("read length more then max length[%d] Bytes",ENTM_RECV_MAX);
|
||||
return -1;
|
||||
}
|
||||
PrivMutexObtain(&agent->lock);
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
agent->read_len = buffer_len;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
// PrivTaskDelay(1000);
|
||||
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime))
|
||||
{
|
||||
//PrivTaskDelay(1000);
|
||||
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("wait sem[%d] timeout\n", agent->entm_rx_notice);
|
||||
printf("wait sem[%d] timeout\n",agent->entm_rx_notice);
|
||||
#endif
|
||||
agent->entm_recv_len = 0;
|
||||
return -1;
|
||||
|
|
@ -410,86 +381,74 @@ static int GetCompleteATReply(ATAgentType agent)
|
|||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
res = PrivRead(agent->fd, &ch, 1);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
if ((res == 1) && (ch != 0))
|
||||
{
|
||||
if((res == 1) && (ch != 0)) {
|
||||
printf(" %c (0x%x)\n", ch, ch);
|
||||
}
|
||||
#endif
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
if (agent->receive_mode == ENTM_MODE)
|
||||
{
|
||||
if (agent->entm_recv_len < ENTM_RECV_MAX)
|
||||
{
|
||||
if (agent->receive_mode == ENTM_MODE) {
|
||||
if (agent->entm_recv_len < ENTM_RECV_MAX) {
|
||||
#ifdef LIB_USING_MQTT
|
||||
if ((res == 1) && (agent->entm_recv_len < agent->read_len))
|
||||
if((res == 1) && (agent->entm_recv_len < agent->read_len))
|
||||
{
|
||||
agent->entm_recv_buf[agent->entm_recv_len] = ch;
|
||||
agent->entm_recv_len++;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#else
|
||||
agent->entm_recv_buf[agent->entm_recv_len] = ch;
|
||||
agent->entm_recv_len++;
|
||||
if (agent->entm_recv_len < agent->read_len)
|
||||
if(agent->entm_recv_len < agent->read_len)
|
||||
{
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
#endif
|
||||
else
|
||||
{
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("ENTM_MODE recv %d Bytes done.\n", agent->entm_recv_len);
|
||||
printf("ENTM_MODE recv %d Bytes done.\n",agent->entm_recv_len);
|
||||
#endif
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
PrivSemaphoreAbandon(&agent->entm_rx_notice);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
} else {
|
||||
printf("entm_recv_buf is_full ...\n");
|
||||
}
|
||||
}
|
||||
else if (agent->receive_mode == AT_MODE)
|
||||
{
|
||||
if (read_len < agent->maintain_max)
|
||||
{
|
||||
if (ch != 0)
|
||||
{ ///< if the char is null then do not save it to the buff
|
||||
else if (agent->receive_mode == AT_MODE) {
|
||||
if (read_len < agent->maintain_max) {
|
||||
if(ch != 0) { ///< if the char is null then do not save it to the buff
|
||||
agent->maintain_buffer[read_len] = ch;
|
||||
read_len++;
|
||||
agent->maintain_len = read_len;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
} else {
|
||||
printf("maintain_len is_full %d ...\n", read_len);
|
||||
is_full = true;
|
||||
}
|
||||
|
||||
if (((ch == '\n') && (agent->reply_lr_end)) ||
|
||||
((ch == '\n') && (last_ch == '\r') && (agent->reply_lr_end)) ||
|
||||
((ch == agent->reply_end_char) && (agent->reply_end_char) &&
|
||||
(last_ch == agent->reply_end_last_char) && (agent->reply_end_last_char)) ||
|
||||
((read_len == agent->reply_char_num) && (agent->reply_char_num)))
|
||||
{
|
||||
if (is_full)
|
||||
{
|
||||
((ch == agent->reply_end_char) && (agent->reply_end_char) &&
|
||||
(last_ch == agent->reply_end_last_char) && (agent->reply_end_last_char)) ||
|
||||
((read_len == agent->reply_char_num) && (agent->reply_char_num))) {
|
||||
if (is_full) {
|
||||
printf("read line failed. The line data length is out of buffer size(%d)!", agent->maintain_max);
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
agent->maintain_len = 0;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
return -1;
|
||||
}
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
|
||||
printf("GetCompleteATReply done\n");
|
||||
#endif
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
break;
|
||||
|
|
@ -505,11 +464,9 @@ static int GetCompleteATReply(ATAgentType agent)
|
|||
|
||||
ATAgentType GetATAgent(const char *agent_name)
|
||||
{
|
||||
struct ATAgent *result = NULL;
|
||||
for (int i = 0; i < AT_AGENT_MAX; i++)
|
||||
{
|
||||
if (strcmp(at_agent_table[i].agent_name, agent_name) == 0)
|
||||
{
|
||||
struct ATAgent* result = NULL;
|
||||
for (int i = 0; i < AT_AGENT_MAX; i++) {
|
||||
if (strcmp(at_agent_table[i].agent_name, agent_name) == 0) {
|
||||
result = &at_agent_table[i];
|
||||
}
|
||||
}
|
||||
|
|
@ -517,51 +474,45 @@ ATAgentType GetATAgent(const char *agent_name)
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
int DeleteATAgent(ATAgentType agent)
|
||||
{
|
||||
printf("delete agent->at_handler = %d\n", agent->at_handler);
|
||||
if (agent->at_handler > 0)
|
||||
{
|
||||
printf("delete agent->at_handler = %d\n",agent->at_handler);
|
||||
if(agent->at_handler > 0){
|
||||
PrivTaskDelete(agent->at_handler, 0);
|
||||
}
|
||||
|
||||
if (agent->fd > 0)
|
||||
{
|
||||
printf("close agent fd = %d\n", agent->fd);
|
||||
if (agent->fd > 0) {
|
||||
printf("close agent fd = %d\n",agent->fd);
|
||||
PrivClose(agent->fd);
|
||||
}
|
||||
|
||||
#ifdef ADD_NUTTX_FEATURES
|
||||
if (agent->lock.sem.semcount > 0)
|
||||
{
|
||||
printf("delete agent lock = %d\n", agent->lock.sem.semcount);
|
||||
if (agent->lock.sem.semcount > 0) {
|
||||
printf("delete agent lock = %d\n",agent->lock.sem.semcount);
|
||||
PrivMutexDelete(&agent->lock);
|
||||
}
|
||||
#elif defined ADD_RTTHREAD_FEATURES
|
||||
#else
|
||||
if (agent->lock)
|
||||
{
|
||||
printf("delete agent lock = %d\n", agent->lock);
|
||||
if (agent->lock) {
|
||||
printf("delete agent lock = %d\n",agent->lock);
|
||||
PrivMutexDelete(&agent->lock);
|
||||
}
|
||||
#endif
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
if (agent->entm_rx_notice)
|
||||
{
|
||||
printf("delete agent entm_rx_notice = %d\n", agent->entm_rx_notice);
|
||||
if (agent->entm_rx_notice) {
|
||||
printf("delete agent entm_rx_notice = %d\n",agent->entm_rx_notice);
|
||||
PrivSemaphoreDelete(&agent->entm_rx_notice);
|
||||
}
|
||||
#else
|
||||
#endif
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
if (agent->rsp_sem)
|
||||
{
|
||||
printf("delete agent rsp_sem = %d\n", agent->rsp_sem);
|
||||
PrivSemaphoreDelete(&agent->rsp_sem);
|
||||
if (agent->rsp_sem) {
|
||||
printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
|
||||
PrivSemaphoreDelete(&agent->rsp_sem);
|
||||
}
|
||||
#endif
|
||||
if (agent->maintain_buffer)
|
||||
{
|
||||
if (agent->maintain_buffer) {
|
||||
PrivFree(agent->maintain_buffer);
|
||||
}
|
||||
|
||||
|
|
@ -574,24 +525,18 @@ static void *ATAgentReceiveProcess(void *param)
|
|||
ATAgentType agent = (ATAgentType)param;
|
||||
const struct at_urc *urc;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (GetCompleteATReply(agent) > 0)
|
||||
{
|
||||
while (1) {
|
||||
if (GetCompleteATReply(agent) > 0) {
|
||||
PrivMutexObtain(&agent->lock);
|
||||
if (agent->reply != NULL)
|
||||
{
|
||||
if (agent->reply != NULL) {
|
||||
ATReplyType reply = agent->reply;
|
||||
|
||||
agent->maintain_buffer[agent->maintain_len] = '\0';
|
||||
if (agent->maintain_len <= reply->reply_max_len)
|
||||
{
|
||||
memset(reply->reply_buffer, 0, reply->reply_max_len);
|
||||
if (agent->maintain_len <= reply->reply_max_len) {
|
||||
memset(reply->reply_buffer, 0 , reply->reply_max_len);
|
||||
memcpy(reply->reply_buffer, agent->maintain_buffer, agent->maintain_len);
|
||||
reply->reply_len = agent->maintain_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("out of memory (%d)!\n", reply->reply_max_len);
|
||||
}
|
||||
|
||||
|
|
@ -610,8 +555,7 @@ static int ATAgentInit(ATAgentType agent)
|
|||
agent->maintain_len = 0;
|
||||
agent->maintain_buffer = (char *)PrivMalloc(agent->maintain_max);
|
||||
|
||||
if (agent->maintain_buffer == NULL)
|
||||
{
|
||||
if (agent->maintain_buffer == NULL) {
|
||||
printf("ATAgentInit malloc maintain_buffer error\n");
|
||||
goto __out;
|
||||
}
|
||||
|
|
@ -619,21 +563,18 @@ static int ATAgentInit(ATAgentType agent)
|
|||
memset(agent->maintain_buffer, 0, agent->maintain_max);
|
||||
|
||||
result = PrivSemaphoreCreate(&agent->entm_rx_notice, 0, 0);
|
||||
if (result < 0)
|
||||
{
|
||||
if (result < 0) {
|
||||
printf("ATAgentInit create entm sem error\n");
|
||||
goto __out;
|
||||
}
|
||||
|
||||
result = PrivSemaphoreCreate(&agent->rsp_sem, 0, 0);
|
||||
if (result < 0)
|
||||
{
|
||||
if (result < 0) {
|
||||
printf("ATAgentInit create rsp sem error\n");
|
||||
goto __out;
|
||||
}
|
||||
|
||||
if (PrivMutexCreate(&agent->lock, 0) < 0)
|
||||
{
|
||||
if(PrivMutexCreate(&agent->lock, 0) < 0) {
|
||||
printf("AdapterFrameworkInit mutex create failed.\n");
|
||||
goto __out;
|
||||
}
|
||||
|
|
@ -650,14 +591,6 @@ static int ATAgentInit(ATAgentType agent)
|
|||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 25;
|
||||
attr.stacksize = 4096;
|
||||
#ifdef ADAPTER_GM800TF
|
||||
attr.schedparam.sched_priority = 16;
|
||||
attr.stacksize = 1800;
|
||||
#endif
|
||||
#ifdef ADAPTER_EC801E
|
||||
attr.schedparam.sched_priority = 16;
|
||||
attr.stacksize = 1800;
|
||||
#endif
|
||||
|
||||
char task_name[] = "at_agent";
|
||||
pthread_args_t args;
|
||||
|
|
@ -683,18 +616,15 @@ int InitATAgent(const char *agent_name, int agent_fd, uint32 maintain_max)
|
|||
int open_result = 0;
|
||||
struct ATAgent *agent = NULL;
|
||||
|
||||
if (GetATAgent(agent_name) != NULL)
|
||||
{
|
||||
if (GetATAgent(agent_name) != NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
while (i < AT_AGENT_MAX && at_agent_table[i].fd > 0)
|
||||
{
|
||||
while (i < AT_AGENT_MAX && at_agent_table[i].fd > 0) {
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i >= AT_AGENT_MAX)
|
||||
{
|
||||
if (i >= AT_AGENT_MAX) {
|
||||
printf("agent buffer(%d) is full.", AT_AGENT_MAX);
|
||||
result = -1;
|
||||
return result;
|
||||
|
|
@ -709,8 +639,7 @@ int InitATAgent(const char *agent_name, int agent_fd, uint32 maintain_max)
|
|||
agent->maintain_max = maintain_max;
|
||||
|
||||
result = ATAgentInit(agent);
|
||||
if (result == 0)
|
||||
{
|
||||
if (result == 0) {
|
||||
PrivTaskStartup(&agent->at_handler);
|
||||
}
|
||||
|
||||
|
|
@ -722,8 +651,7 @@ ATReplyType CreateATReply(uint32 reply_max_len)
|
|||
ATReplyType reply = NULL;
|
||||
|
||||
reply = (ATReplyType)PrivMalloc(sizeof(struct ATReply));
|
||||
if (reply == NULL)
|
||||
{
|
||||
if (reply == NULL) {
|
||||
printf("no more memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -732,8 +660,7 @@ ATReplyType CreateATReply(uint32 reply_max_len)
|
|||
reply->reply_max_len = reply_max_len;
|
||||
|
||||
reply->reply_buffer = (char *)PrivMalloc(reply_max_len);
|
||||
if (reply->reply_buffer == NULL)
|
||||
{
|
||||
if (reply->reply_buffer == NULL) {
|
||||
printf("no more memory\n");
|
||||
PrivFree(reply);
|
||||
return NULL;
|
||||
|
|
@ -746,17 +673,14 @@ ATReplyType CreateATReply(uint32 reply_max_len)
|
|||
|
||||
void DeleteATReply(ATReplyType reply)
|
||||
{
|
||||
if (reply)
|
||||
{
|
||||
if (reply->reply_buffer)
|
||||
{
|
||||
if (reply) {
|
||||
if (reply->reply_buffer) {
|
||||
PrivFree(reply->reply_buffer);
|
||||
reply->reply_buffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (reply)
|
||||
{
|
||||
if (reply) {
|
||||
PrivFree(reply);
|
||||
reply = NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,15 +58,9 @@ struct ATAgent
|
|||
uint32 maintain_len;
|
||||
uint32 maintain_max;
|
||||
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
#if defined(ADAPTER_GM800TF) || defined(ADAPTER_EC801E)
|
||||
pthread_mutex_t lock;
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
int lock;
|
||||
#else
|
||||
int lock;
|
||||
#endif
|
||||
|
||||
#else // ADD_XIZI_FEATURES
|
||||
pthread_mutex_t lock;
|
||||
#endif
|
||||
|
||||
|
|
@ -75,7 +69,7 @@ struct ATAgent
|
|||
char reply_end_last_char;
|
||||
char reply_end_char;
|
||||
uint32 reply_char_num;
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
int rsp_sem;
|
||||
#else
|
||||
sem_t rsp_sem;
|
||||
|
|
|
|||
|
|
@ -3,13 +3,7 @@ config ADAPTER_HFA21_ETHERNET
|
|||
Please check HFA21 can only work for adapter_wifi or adapter_ethernet in the meantime!
|
||||
bool "Using ethernet adapter device HFA21"
|
||||
default n
|
||||
config ADAPTER_WCHNET_ETHERNET
|
||||
bool "Using ethernet adapter device WCHNET"
|
||||
default n
|
||||
|
||||
if ADAPTER_HFA21_ETHERNET
|
||||
source "$APP_DIR/Framework/connection/ethernet/hfa21_ethernet/Kconfig"
|
||||
endif
|
||||
if ADAPTER_WCHNET_ETHERNET
|
||||
source "$APP_DIR/Framework/connection/ethernet/wchnet_ethernet/Kconfig"
|
||||
endif
|
||||
|
|
|
|||
|
|
@ -3,8 +3,5 @@ SRC_FILES := adapter_ethernet.c
|
|||
ifeq ($(CONFIG_ADAPTER_HFA21_ETHERNET),y)
|
||||
SRC_DIR += hfa21_ethernet
|
||||
endif
|
||||
ifeq ($(CONFIG_ADAPTER_WCHNET_ETHERNET),y)
|
||||
SRC_DIR += wchnet_ethernet
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file adapter_ethernet.c
|
||||
|
|
@ -23,11 +23,9 @@
|
|||
#ifdef ADAPTER_HFA21_ETHERNET
|
||||
extern AdapterProductInfoType Hfa21EthernetAttach(struct Adapter *adapter);
|
||||
#endif
|
||||
#ifdef ADAPTER_WCHNET_ETHERNET
|
||||
extern AdapterProductInfoType wchnetEthernetAttach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
static int AdapterEthernetRegister(struct Adapter *adapter) {
|
||||
static int AdapterEthernetRegister(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
strncpy(adapter->name, ADAPTER_ETHERNET_NAME, NAME_NUM_MAX);
|
||||
|
|
@ -44,7 +42,8 @@ static int AdapterEthernetRegister(struct Adapter *adapter) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
int AdapterEthernetInit(void) {
|
||||
int AdapterEthernetInit(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter));
|
||||
|
|
@ -75,28 +74,17 @@ int AdapterEthernetInit(void) {
|
|||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
#ifdef ADAPTER_WCHNET_ETHERNET
|
||||
AdapterProductInfoType product_info = wchnetEthernetAttach(adapter);
|
||||
if (!product_info) {
|
||||
printf("AdapterEthernetInit wchnet attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************ethernet TEST*********************/
|
||||
int AdapterEthernetTest(void) {
|
||||
int AdapterEthernetTest(void)
|
||||
{
|
||||
int baud_rate = BAUD_RATE_57600;
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME);
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME);
|
||||
|
||||
#ifdef ADAPTER_HFA21_ETHERNET
|
||||
|
||||
|
|
@ -108,67 +96,30 @@ int AdapterEthernetTest(void) {
|
|||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
|
||||
AdapterDeviceSetUp(adapter);
|
||||
|
||||
|
||||
const char *ip = "192.168.131.26";
|
||||
const char *port = "9999";
|
||||
enum NetRoleType net_role = CLIENT; // SERVER
|
||||
enum NetRoleType net_role = CLIENT;//SERVER
|
||||
enum IpType ip_type = IPV4;
|
||||
AdapterDeviceConnect(adapter, net_role, ip, port, ip_type);
|
||||
|
||||
printf("ready to test data transfer\n");
|
||||
PrivTaskDelay(2000);
|
||||
len = strlen(ethernet_msg);
|
||||
for (i = 0; i < 10; i++) {
|
||||
for (i = 0;i < 10; i ++) {
|
||||
printf("AdapterEthernetTest send %s\n", ethernet_msg);
|
||||
AdapterDeviceSend(adapter, ethernet_msg, len);
|
||||
PrivTaskDelay(4000);
|
||||
}
|
||||
|
||||
|
||||
while (1) {
|
||||
AdapterDeviceRecv(adapter, ethernet_recv_msg, 128);
|
||||
printf("AdapterEthernetTest recv %s\n", ethernet_recv_msg);
|
||||
memset(ethernet_recv_msg, 0, 128);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
#ifdef ADAPTER_WCHNET_ETHERNET
|
||||
AdapterDeviceSetUp(adapter);
|
||||
// AdapterDeviceSetAddr(adapter, "192.168.1.10", "255.255.255.0", "192.168.1.1");
|
||||
// AdapterDeviceSetDhcp(adapter, 1);
|
||||
// AdapterDeviceConnect(adapter, CLIENT, "115.238.53.59", "10208", IPV4);
|
||||
AdapterDeviceConnect(adapter, CLIENT, "192.168.1.100", "1000", IPV4);
|
||||
AdapterDeviceSend(adapter, "Hello World!", sizeof("Hello World!"));
|
||||
// /* 测试DHCP连接路由器后,连接因特网 */
|
||||
// AdapterDeviceSend(adapter,
|
||||
// "GET /v3/weather/weatherInfo?city=%E9%95%BF%E6%B2%99&key=13cb58f5884f9749287abbead9c658f2 "
|
||||
// "HTTP/1.1\r\nHost: restapi.amap.com\r\n\r\n",
|
||||
// sizeof("GET
|
||||
// /v3/weather/weatherInfo?city=%E9%95%BF%E6%B2%99&key=13cb58f5884f9749287abbead9c658f2 "
|
||||
// "HTTP/1.1\r\nHost: restapi.amap.com\r\n\r\n"));
|
||||
char receiveBuffer[128] = {};
|
||||
PrivTaskDelay(20000);
|
||||
ssize_t readLength = AdapterDeviceRecv(adapter, receiveBuffer, sizeof(receiveBuffer));
|
||||
// printf("receiveBuffer:%s\n", receiveBuffer);
|
||||
printf("[socketid-%d]receiveBuffer:", adapter->socket.socket_id);
|
||||
for (int i = 0; i < readLength; i++) {
|
||||
printf("%c", receiveBuffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
AdapterDeviceDisconnect(adapter, NULL);
|
||||
// AdapterDeviceConnect(adapter, CLIENT, "115.238.53.59", "10208", IPV4);
|
||||
AdapterDeviceConnect(adapter, CLIENT, "192.168.1.100", "1000", IPV4);
|
||||
AdapterDeviceSend(adapter, "Hello World!", sizeof("Hello World!"));
|
||||
PrivTaskDelay(20000);
|
||||
readLength = AdapterDeviceRecv(adapter, receiveBuffer, sizeof(receiveBuffer));
|
||||
printf("[socketid-%d]receiveBuffer:", adapter->socket.socket_id);
|
||||
for (int i = 0; i < readLength; i++) {
|
||||
printf("%c", receiveBuffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
AdapterDeviceSetDown(adapter);
|
||||
AdapterDeviceClose(adapter);
|
||||
#endif
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(AdapterEthernetTest, a ethernet test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
config ADAPTER_ETHERNET_WCHNET
|
||||
string "WCHNET ETHERNET adapter name"
|
||||
default "wchnet_ethernet"
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
SRC_FILES := wchnet_ethernet.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
from building import *
|
||||
import os
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = []
|
||||
if GetDepend(['ADAPTER_WCHNET_ETHERNET']):
|
||||
src += ['wchnet_ethernet.c']
|
||||
group = DefineGroup('connection ethernet wchnet', src, depend = [], CPPPATH = [cwd])
|
||||
|
||||
Return('group')
|
||||
|
|
@ -1,532 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file wchnet_ethernet.c
|
||||
* @brief Implement the connection ethernet adapter function, using wchnet device
|
||||
* @author Huo Yujia (huoyujia081@126.com)
|
||||
* @version 1.0
|
||||
* @date 2024-07-17
|
||||
*/
|
||||
|
||||
#include <ModuleConfig.h>
|
||||
#include <adapter.h>
|
||||
#include <connect_ether.h>
|
||||
#include <eth_driver.h>
|
||||
|
||||
extern pmodule_cfg CFG; // 从flash中读取的配置信息指针
|
||||
|
||||
/* 记录socket会话参数,由于ch32v208内存限制,目前只支持单个会话通信 */
|
||||
struct WchnetSocketConfiguration wchnetSocketConfiguration;
|
||||
|
||||
/**
|
||||
* @brief wchnet主任务线程,主要用于不断处理以太网中断
|
||||
* @param argv 参数无意义
|
||||
* @return void* 返回值无意义
|
||||
*/
|
||||
static void *wchnetMainTask(void *argv) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration = (struct WchnetSocketConfiguration *)argv;
|
||||
while (1) {
|
||||
PrivTaskDelay(50);
|
||||
WCHNET_MainTask();
|
||||
if (WCHNET_QueryGlobalInt()) {
|
||||
WCHNET_HandleGlobalInt(pWchnetSocketConfiguration);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 以太网发送数据
|
||||
* @param adapter
|
||||
* @param data 所要发送数据的存储区域指针
|
||||
* @param len 所要发送数据的长度
|
||||
* @return int 实际发送的数据长度
|
||||
*/
|
||||
static int wchnetEthernetSend(struct Adapter *adapter, const void *data, size_t len) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
/* 判断是否可以发送数据 */
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) { // 判断是否已启动wchnet主任务线程
|
||||
printf("[error %s %d]wchnetMainThread has not been started, please call function AdapterDeviceSetUp first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
} else if (pWchnetSocketConfiguration->socketStatus != WCHNET_SOCKET_CONNECT_SUCCESS) { // 判断是否已连接socket
|
||||
printf(
|
||||
"[error %s %d]socket has not been connected, please call function AdapterDeviceConnect "
|
||||
"first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 发送数据 */
|
||||
uint32_t tempLen = (uint32_t)len; // 实际发送数据长度
|
||||
uint8_t res = WCHNET_SocketSend(pWchnetSocketConfiguration->socketId, (uint8_t *)data, &tempLen);
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
printf("[socketid-%d]send data success, len = %d\n", adapter->socket.socket_id, tempLen);
|
||||
return tempLen;
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 以太网接收数据
|
||||
* @param adapter
|
||||
* @param rev_buffer 接收数据首地址
|
||||
* @param buffer_len 期望读取的数据长度
|
||||
* @return int 实际读取的数据长度
|
||||
*/
|
||||
static int wchnetEthernetReceive(struct Adapter *adapter, void *rev_buffer, size_t buffer_len) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
/* 判断是否可以接收数据 */
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) { // 判断是否已启动wchnet主任务线程
|
||||
printf("[error %s %d]wchnetMainThread has not been started, please call function AdapterDeviceSetUp first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 接收数据 */
|
||||
uint32_t tempLen = (uint32_t)buffer_len; // 实际接收数据长度
|
||||
uint8_t res = WCHNET_SocketRecv(pWchnetSocketConfiguration->socketId, (u8 *)rev_buffer, &tempLen);
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
printf("[socketid-%d]recv data success, len = %d\n", pWchnetSocketConfiguration->socketId, tempLen);
|
||||
return tempLen;
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 启动wchnet主任务线程,监听以太网中断
|
||||
* @param adapter
|
||||
* @return int 0表示线程启动成功,其他表示线程启动失败
|
||||
*/
|
||||
static int wchnetEthernetSetUp(struct Adapter *adapter) {
|
||||
pthread_attr_t wchnetMainTaskAttr;
|
||||
pthread_args_t wchnetMainTaskArgs;
|
||||
wchnetMainTaskAttr.schedparam.sched_priority = 16; // 线程优先级
|
||||
wchnetMainTaskAttr.stacksize = 1100; // 线程栈大小
|
||||
wchnetMainTaskArgs.pthread_name = "wchnetMainTask"; // 线程名字
|
||||
wchnetMainTaskArgs.arg = &wchnetSocketConfiguration; // 线程参数
|
||||
int res = PrivTaskCreate(&wchnetSocketConfiguration.wchnetMainThread, &wchnetMainTaskAttr, wchnetMainTask,
|
||||
&wchnetMainTaskArgs);
|
||||
PrivTaskStartup(&wchnetSocketConfiguration.wchnetMainThread);
|
||||
adapter->adapter_param = &wchnetSocketConfiguration;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 关闭以太网socket连接
|
||||
* @param adapter
|
||||
* @return int 0表示关闭以太网socket连接成功,其他表示关闭以太网socket连接失败
|
||||
*/
|
||||
static int wchnetEthernetDisconnect(struct Adapter *adapter) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
/* 判断是否可以关闭socket */
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) { // 判断是否已启动wchnet主任务线程
|
||||
printf("[error %s %d]wchnetMainThread has not been started, please call function AdapterDeviceSetUp first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
} else if (pWchnetSocketConfiguration->socketStatus !=
|
||||
WCHNET_SOCKET_CONNECT_SUCCESS) { // 判断当前是否已有socket连接
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
/* 将目的IP地址和端口号的数组形式转换成字符串形式和unsigned short形式 */
|
||||
char destinationIpAddress[16]; // 目的IP地址
|
||||
uint16_t destinationPort; // 目的端口号
|
||||
sprintf(destinationIpAddress, "%u.%u.%u.%u", pWchnetSocketConfiguration->destinationIpAddress[0],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[1], pWchnetSocketConfiguration->destinationIpAddress[2],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[3]);
|
||||
destinationPort =
|
||||
(pWchnetSocketConfiguration->destinationPort[1] << 8) + pWchnetSocketConfiguration->destinationPort[0];
|
||||
|
||||
/* 关闭socket连接 */
|
||||
uint8_t res = WCHNET_SocketClose(pWchnetSocketConfiguration->socketId, TCP_CLOSE_NORMAL);
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
while (pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_CONNECT_SUCCESS) {
|
||||
PrivTaskDelay(100); // 每100ms检查一次是否socket关闭完成
|
||||
}
|
||||
/* socket关闭完成 */
|
||||
pWchnetSocketConfiguration->socketStatus = WCHNET_SOCKET_UNDEFINED;
|
||||
printf("[socketid-%d]socket with %s:%hu has been disconnected\n", pWchnetSocketConfiguration->socketId,
|
||||
destinationIpAddress, destinationPort);
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
printf("[socketid-%d]socket with %s:%hu disconnect error\n", pWchnetSocketConfiguration->socketId,
|
||||
destinationIpAddress, destinationPort);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 建立以太网socket连接
|
||||
* @param adapter
|
||||
* @param net_role 网络角色,CLIENT或者SERVER
|
||||
* @param ip 目的IP地址字符串
|
||||
* @param port 目的端口号字符串
|
||||
* @param ip_type IP类型,IPV4或者IPV6
|
||||
* @return int 0表示建立以太网socket连接成功,其他表示建立以太网socket连接失败
|
||||
*/
|
||||
static int wchnetEthernetConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port,
|
||||
enum IpType ip_type) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
/* 由于ch32v208内存限制,当前仅支持IPV4+CLIENT */
|
||||
if (ip_type != IPV4) {
|
||||
printf("[error %s %d]wchnetEthernetConnect only support IPV4, do not support IPV6\n", __func__, __LINE__);
|
||||
return -1;
|
||||
} else if (net_role != CLIENT) {
|
||||
printf("[error %s %d]wchnetEthernetConnect only support CLIENT, do not support SERVER\n", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 将IP地址和端口号的字符串形式转换成数组形式 */
|
||||
uint8_t destinationIpAddress[4];
|
||||
uint8_t destinationPort[2];
|
||||
WCHNET_Aton(ip, destinationIpAddress);
|
||||
destinationPort[0] = atoi(port) % 256;
|
||||
destinationPort[1] = atoi(port) / 256;
|
||||
|
||||
uint8_t res = WCHNET_ERR_SUCCESS;
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) { // 判断是否已启动wchnet主任务线程
|
||||
printf("[error %s %d]wchnetMainThread has not been started, please call function AdapterDeviceSetUp first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
} else if (pWchnetSocketConfiguration->socketStatus ==
|
||||
WCHNET_SOCKET_CONNECT_SUCCESS) { // 判断当前是否已有socket连接
|
||||
if (memcmp(destinationIpAddress, pWchnetSocketConfiguration->destinationIpAddress, 4) == 0 &&
|
||||
memcmp(destinationPort, pWchnetSocketConfiguration->destinationPort, 2) ==
|
||||
0) { // 当前连接的socket,目的IP地址和端口号完全一致,无需再次连接
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
} else { // 当前连接的socket,目的IP地址和端口号不完全一致,由于ch32v208内存限制,当前仅支持单个socket通信,需断开当前连接
|
||||
res = wchnetEthernetDisconnect(adapter);
|
||||
if (res != WCHNET_ERR_SUCCESS) {
|
||||
mStopIfError(res);
|
||||
printf("[error %s %d]wchnetEthernetDisconnect fail\n", __func__, __LINE__);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 保存目的IP地址和目的端口号 */
|
||||
memcpy(pWchnetSocketConfiguration->destinationIpAddress, destinationIpAddress, 4);
|
||||
memcpy(pWchnetSocketConfiguration->destinationPort, destinationPort, 2);
|
||||
pWchnetSocketConfiguration->socketStatus = WCHNET_SOCKET_UNDEFINED;
|
||||
|
||||
/* 配置socket会话 */
|
||||
SOCK_INF TmpSocketInf;
|
||||
memset((void *)&TmpSocketInf, 0, sizeof(SOCK_INF));
|
||||
memcpy(TmpSocketInf.IPAddr, destinationIpAddress, 4);
|
||||
TmpSocketInf.SourPort =
|
||||
(pWchnetSocketConfiguration->sourcePort[1] << 8) + pWchnetSocketConfiguration->sourcePort[0]; // 源端口号
|
||||
TmpSocketInf.DesPort = (pWchnetSocketConfiguration->destinationPort[1] << 8) +
|
||||
pWchnetSocketConfiguration->destinationPort[0]; // 目的端口号
|
||||
TmpSocketInf.ProtoType = PROTO_TYPE_TCP; // 协议类型,TCP
|
||||
TmpSocketInf.RecvBufLen = RECE_BUF_LEN; // 接收缓冲区大小
|
||||
|
||||
/* 创建socket */
|
||||
res = WCHNET_SocketCreat(&pWchnetSocketConfiguration->socketId, &TmpSocketInf);
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
printf("[socketid-%d]create socketId %d\r\n", pWchnetSocketConfiguration->socketId,
|
||||
pWchnetSocketConfiguration->socketId);
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
printf("[error %s %d]WCHNET_SocketCreat fail\n", __func__, __LINE__);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 连接socket */
|
||||
res = WCHNET_SocketConnect(pWchnetSocketConfiguration->socketId);
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
printf("[socketid-%d]connecting socketId %d\r\n", pWchnetSocketConfiguration->socketId,
|
||||
pWchnetSocketConfiguration->socketId);
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
printf("[error %s %d]WCHNET_SocketConnect fail\n", __func__, __LINE__);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 每100ms查询一次,直至连接成功或者超时 */
|
||||
while (pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_UNDEFINED) {
|
||||
printf("[socketid-%d]connecting socketId %d\r\n", pWchnetSocketConfiguration->socketId,
|
||||
pWchnetSocketConfiguration->socketId);
|
||||
PrivTaskDelay(1000);
|
||||
}
|
||||
if (pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_CONNECT_SUCCESS) {
|
||||
printf("[socketid-%d]socket connect success\n", pWchnetSocketConfiguration->socketId);
|
||||
return res;
|
||||
} else if (pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_CONNECT_TIMEOUT) {
|
||||
printf("[socketid-%d]socket connect timeout\n", pWchnetSocketConfiguration->socketId);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置以太网源IP地址,网关地址,子网掩码
|
||||
* @param adapter
|
||||
* @param ip 源IP地址
|
||||
* @param gateway 网关地址
|
||||
* @param netmask 子网掩码
|
||||
* @return int 0表示设置成功,其他表示设置失败
|
||||
*/
|
||||
static int wchnetEthernetSetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
/* 判断是否已启动wchnet主任务线程 */
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) {
|
||||
printf("[error %s %d]wchnetMainThread has not been started, please call function AdapterDeviceSetUp first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 将地址的字符串形式转化为数组形式 */
|
||||
uint8_t sourceIpAddress[4], sourceGateway[4], sourceSubnetMask[4];
|
||||
WCHNET_Aton(ip, sourceIpAddress);
|
||||
WCHNET_Aton(gateway, sourceGateway);
|
||||
WCHNET_Aton(netmask, sourceSubnetMask);
|
||||
|
||||
/* 若配置内容和当前配置保持一致,则无需配置 */
|
||||
if (memcmp(sourceIpAddress, pWchnetSocketConfiguration->sourceIpAddress, 4) == 0 &&
|
||||
memcmp(sourceGateway, pWchnetSocketConfiguration->sourceGateway, 4) == 0 &&
|
||||
memcmp(sourceSubnetMask, pWchnetSocketConfiguration->sourceSubnetMask, 4) == 0) {
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
/* 若当前有socket正在连接,则需断开socket,再使用新的配置连接socket */
|
||||
uint8_t socketExists = pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_CONNECT_SUCCESS;
|
||||
if (socketExists) {
|
||||
wchnetEthernetDisconnect(adapter);
|
||||
}
|
||||
|
||||
/* 进行配置,并重新初始化wchnet */
|
||||
memcpy(pWchnetSocketConfiguration->sourceIpAddress, sourceIpAddress, 4);
|
||||
memcpy(pWchnetSocketConfiguration->sourceGateway, sourceGateway, 4);
|
||||
memcpy(pWchnetSocketConfiguration->sourceSubnetMask, sourceSubnetMask, 4);
|
||||
uint8_t res = WCHNET_Init(pWchnetSocketConfiguration->sourceIpAddress, pWchnetSocketConfiguration->sourceGateway,
|
||||
pWchnetSocketConfiguration->sourceSubnetMask, pWchnetSocketConfiguration->macAddress);
|
||||
|
||||
if (res == WCHNET_ERR_SUCCESS) { // 初始化wchnet成功
|
||||
pWchnetSocketConfiguration->dhcpSwitch = 0;
|
||||
pWchnetSocketConfiguration->dhcpStatus = WCHNET_DHCP_UNDEFINED;
|
||||
printf("-*-*-*wchnetEthernetSetAddr*-*-*\n");
|
||||
printf("ip:\t%s\ngateway:%s\nnetmask:%s\n", ip, gateway, netmask);
|
||||
printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
|
||||
if (socketExists) { // 若之前有socket正在连接,则需重新连接
|
||||
char destinationIpAddress[16], destinationPort[6];
|
||||
sprintf(destinationIpAddress, "%u.%u.%u.%u", pWchnetSocketConfiguration->destinationIpAddress[0],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[1],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[2],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[3]);
|
||||
sprintf(
|
||||
destinationPort, "%hu",
|
||||
(pWchnetSocketConfiguration->destinationPort[1] << 8) + pWchnetSocketConfiguration->destinationPort[0]);
|
||||
res = wchnetEthernetConnect(adapter, CLIENT, destinationIpAddress, destinationPort, IPV4);
|
||||
}
|
||||
}
|
||||
mStopIfError(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 删除wchnet主任务线程
|
||||
* @param adapter
|
||||
* @return int 0表示删除wchnet主任务线程成功,其他表示删除wchnet主任务线程失败
|
||||
*/
|
||||
static int wchnetEthernetSetDown(struct Adapter *adapter) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
/* 如果wchnet主任务线程已经处于关闭状态 */
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) {
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t res = WCHNET_ERR_SUCCESS;
|
||||
|
||||
/* 如果仍有socket未关闭,先关闭socket */
|
||||
if (pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_CONNECT_SUCCESS) {
|
||||
res = wchnetEthernetDisconnect(adapter);
|
||||
if (res != WCHNET_ERR_SUCCESS) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* 删除wchnet主任务线程 */
|
||||
res = PrivTaskDelete(pWchnetSocketConfiguration->wchnetMainThread, 0);
|
||||
if (res != 0) {
|
||||
printf("wchnet ethernet set down fail\n");
|
||||
} else {
|
||||
pWchnetSocketConfiguration->wchnetMainThread = 0;
|
||||
printf("wchnet ethernet set down success\n");
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 设置DHCP
|
||||
* @param adapter
|
||||
* @param enable 0表示关闭DHCP,1表示打开DHCP
|
||||
* @return int 0表示设置成功,其他表示设置失败
|
||||
* @note 若获得新的动态IP地址,则关闭当前的所有socket连接,并使用新的IP地址建立连接
|
||||
* @note 若获得的动态IP地址和当前的IP地址一致,则不进行任何操作,继续使用旧的IP地址进行通信
|
||||
* @note 若获取动态IP地址失败,则继续使用旧的IP地址进行通信
|
||||
*/
|
||||
static int wchnetEthernetSetDHCP(struct Adapter *adapter, int enable) {
|
||||
struct WchnetSocketConfiguration *pWchnetSocketConfiguration =
|
||||
(struct WchnetSocketConfiguration *)adapter->adapter_param;
|
||||
|
||||
if (pWchnetSocketConfiguration->wchnetMainThread == 0) { // 判断是否已启动wchnet主任务线程
|
||||
printf("[error %s %d]wchnetMainThread has not been started, please call function AdapterDeviceSetUp first\n",
|
||||
__func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t res;
|
||||
switch (enable) {
|
||||
/* 关闭DHCP */
|
||||
case 0:
|
||||
/* 如果原来就没有打开DHCP,无需关闭DHCP */
|
||||
if (pWchnetSocketConfiguration->dhcpSwitch == 0) {
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
/* 重新使用静态IP地址初始化,相当于关闭DHCP */
|
||||
res = WCHNET_Init(pWchnetSocketConfiguration->sourceIpAddress, pWchnetSocketConfiguration->sourceGateway,
|
||||
pWchnetSocketConfiguration->sourceSubnetMask, pWchnetSocketConfiguration->macAddress);
|
||||
if (res != WCHNET_ERR_SUCCESS) { // DHCP关闭失败
|
||||
mStopIfError(res);
|
||||
} else { // DHCP关闭成功
|
||||
pWchnetSocketConfiguration->dhcpSwitch = 0;
|
||||
pWchnetSocketConfiguration->dhcpStatus = WCHNET_DHCP_UNDEFINED;
|
||||
}
|
||||
return res;
|
||||
break;
|
||||
|
||||
/* 打开DHCP */
|
||||
case 1:
|
||||
/* 如果原来已经打开DHCP */
|
||||
if (pWchnetSocketConfiguration->dhcpSwitch == 1 &&
|
||||
(pWchnetSocketConfiguration->dhcpStatus == WCHNET_DHCP_SUCCESS_NEW ||
|
||||
pWchnetSocketConfiguration->dhcpStatus == WCHNET_DHCP_SUCCESS_OLD)) {
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
/* 配置DHCP主机名 */
|
||||
res = WCHNET_DHCPSetHostname("WCHNET");
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
printf("DHCP hostname set success\n");
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
printf("[error %s %d]WCHNET_DHCPSetHostname fail\n", __func__, __LINE__);
|
||||
}
|
||||
|
||||
/* 开启DHCP */
|
||||
res = WCHNET_DHCPStart(WCHNET_DHCPCallBack);
|
||||
if (res == WCHNET_ERR_SUCCESS) {
|
||||
pWchnetSocketConfiguration->dhcpStatus = WCHNET_DHCP_UNDEFINED;
|
||||
while (pWchnetSocketConfiguration->dhcpStatus == WCHNET_DHCP_UNDEFINED) {
|
||||
PrivTaskDelay(1000); // 每秒查询一次DHCP状态
|
||||
printf("DHCP finding IP\n");
|
||||
}
|
||||
WCHNET_DHCPStop(); // 停止DHCP
|
||||
switch (pWchnetSocketConfiguration->dhcpStatus) {
|
||||
case WCHNET_DHCP_SUCCESS_NEW: // 获取到新的动态IP地址,若之前连接过socket,则重新连接该socket
|
||||
pWchnetSocketConfiguration->dhcpSwitch = 1;
|
||||
printf("DHCP set up successfully, getting new IP configuration\n");
|
||||
if (pWchnetSocketConfiguration->socketStatus == WCHNET_SOCKET_CONNECT_SUCCESS) {
|
||||
char destinationIpAddress[16];
|
||||
char destinationPort[6];
|
||||
sprintf(destinationIpAddress, "%u.%u.%u.%u",
|
||||
pWchnetSocketConfiguration->destinationIpAddress[0],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[1],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[2],
|
||||
pWchnetSocketConfiguration->destinationIpAddress[3]);
|
||||
sprintf(destinationPort, "%hu",
|
||||
(pWchnetSocketConfiguration->destinationPort[1] << 8) +
|
||||
pWchnetSocketConfiguration->destinationPort[0]);
|
||||
if (!wchnetEthernetDisconnect(adapter) &&
|
||||
!wchnetEthernetConnect(adapter, CLIENT, destinationIpAddress, destinationPort, IPV4)) {
|
||||
printf("[socketid-%d]DHCP: socket with %s:%s has been reset\n",
|
||||
pWchnetSocketConfiguration->socketId, destinationIpAddress, destinationPort);
|
||||
} else {
|
||||
printf("[socketid-%d]DHCP: socket with %s:%s reset failed\n",
|
||||
pWchnetSocketConfiguration->socketId, pWchnetSocketConfiguration->socketId,
|
||||
destinationIpAddress, destinationPort);
|
||||
}
|
||||
}
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
break;
|
||||
case WCHNET_DHCP_SUCCESS_OLD: // 获取到旧的动态IP地址
|
||||
pWchnetSocketConfiguration->dhcpSwitch = 1;
|
||||
printf("DHCP set up successfully, getting the same IP configuration\n");
|
||||
return WCHNET_ERR_SUCCESS;
|
||||
break;
|
||||
case WCHNET_DHCP_FAIL: // 获取动态IP地址错误
|
||||
pWchnetSocketConfiguration->dhcpSwitch = 0;
|
||||
printf("[error %s %d]wchnet dhcp fail\n", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
mStopIfError(res);
|
||||
return res;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct IpProtocolDone wchnet_ethernet_done = {
|
||||
.open = NULL,
|
||||
.close = NULL,
|
||||
.ioctl = NULL,
|
||||
.setup = wchnetEthernetSetUp,
|
||||
.setdown = wchnetEthernetSetDown,
|
||||
.setaddr = wchnetEthernetSetAddr,
|
||||
.setdns = NULL,
|
||||
.setdhcp = wchnetEthernetSetDHCP,
|
||||
.ping = NULL,
|
||||
.netstat = NULL,
|
||||
.connect = wchnetEthernetConnect,
|
||||
.send = wchnetEthernetSend,
|
||||
.recv = wchnetEthernetReceive,
|
||||
.disconnect = wchnetEthernetDisconnect,
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: Register ethernet device wchnet
|
||||
* @return success: product_info, failure: NULL
|
||||
*/
|
||||
AdapterProductInfoType wchnetEthernetAttach(struct Adapter *adapter) {
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("wchnetEthernetAttach Attach malloc product_info error\n");
|
||||
PrivFree(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(product_info->model_name, ADAPTER_ETHERNET_WCHNET);
|
||||
|
||||
product_info->model_done = (void *)&wchnet_ethernet_done;
|
||||
|
||||
return product_info;
|
||||
}
|
||||
|
|
@ -977,7 +977,7 @@ int AdapterLoraTest(void)
|
|||
char task_name_2[] = "adapter_lora_gateway";
|
||||
args.pthread_name = task_name_2;
|
||||
args.arg = (void *)adapter;
|
||||
PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)&args);
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_gateway_attr, &LoraReceiveTask, (void *)&args);
|
||||
#endif
|
||||
|
||||
PrivTaskStartup(&lora_gateway_task);
|
||||
|
|
|
|||
|
|
@ -17,5 +17,4 @@ menu "app lib"
|
|||
source "$APP_DIR/lib/mqtt/Kconfig"
|
||||
source "$APP_DIR/lib/JerryScript/Kconfig"
|
||||
source "$APP_DIR/lib/SQLite/Kconfig"
|
||||
source "$APP_DIR/lib/pikapython/Kconfig"
|
||||
endmenu
|
||||
|
|
|
|||
|
|
@ -30,10 +30,6 @@ ifeq ($(CONFIG_LIB_USING_SQLITE),y)
|
|||
SRC_DIR += SQLite
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIB_USING_PIKAPYTHON),y)
|
||||
SRC_DIR += pikapython
|
||||
endif
|
||||
|
||||
# ifeq ($(CONFIG_LIB_USING_FREEMODBUS),y)
|
||||
# SRC_DIR += freemodbus
|
||||
# endif
|
||||
|
|
|
|||
|
|
@ -178,8 +178,7 @@ bool MQTT_Connect(void)
|
|||
MQTT_Send(Platform_mqtt.Pack_buff,Platform_mqtt.Fixed_len + Platform_mqtt.Variable_len + Platform_mqtt.Payload_len);
|
||||
MdelayKTask(50);
|
||||
MQTT_Recv(mqtt_rxbuf, 4);
|
||||
if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]
|
||||
&& mqtt_rxbuf[2] == parket_connetAck[2] && mqtt_rxbuf[3] == parket_connetAck[3]) //连接成功
|
||||
if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]) //连接成功
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
menu "lib using PikaPython"
|
||||
menuconfig LIB_USING_PIKAPYTHON
|
||||
bool "Enable support PikaPython."
|
||||
default n
|
||||
|
||||
if LIB_USING_PIKAPYTHON
|
||||
# PikaPython configuration options
|
||||
# Note: PikaPython uses its own internal configuration system
|
||||
endif
|
||||
endmenu
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
# PikaPython Makefile for XiUOS
|
||||
|
||||
# IMPORTANT:
|
||||
# This build system expects SRC_FILES (list of .c files) or recurses into sub-makefiles.
|
||||
# To ensure files are compiled, enumerate source files explicitly.
|
||||
|
||||
SRC_FILES :=
|
||||
|
||||
ifeq ($(CONFIG_LIB_USING_PIKAPYTHON),y)
|
||||
# PikaPython core files
|
||||
SRC_FILES += pikascript-core/PikaVM.c
|
||||
SRC_FILES += pikascript-core/PikaObj.c
|
||||
SRC_FILES += pikascript-core/PikaParser.c
|
||||
SRC_FILES += pikascript-core/PikaCompiler.c
|
||||
SRC_FILES += pikascript-core/BaseObj.c
|
||||
SRC_FILES += pikascript-core/dataArg.c
|
||||
SRC_FILES += pikascript-core/dataArgs.c
|
||||
SRC_FILES += pikascript-core/dataLink.c
|
||||
SRC_FILES += pikascript-core/dataLinkNode.c
|
||||
SRC_FILES += pikascript-core/dataMemory.c
|
||||
SRC_FILES += pikascript-core/dataQueue.c
|
||||
SRC_FILES += pikascript-core/dataQueueObj.c
|
||||
SRC_FILES += pikascript-core/dataStack.c
|
||||
SRC_FILES += pikascript-core/dataString.c
|
||||
SRC_FILES += pikascript-core/dataStrs.c
|
||||
SRC_FILES += pikascript-core/TinyObj.c
|
||||
SRC_FILES += pikascript-core/PikaPlatform.c
|
||||
|
||||
# Pre-compiled Python API files
|
||||
SRC_FILES += pikascript-api/pikaScript.c
|
||||
SRC_FILES += pikascript-api/__pikaBinding.c
|
||||
SRC_FILES += pikascript-api/__asset_pikaModules_py_a.c
|
||||
|
||||
# PikaStdLib files (basic functionality)
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdLib_StringObj.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_ByteArray.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_Dict.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_List.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_String.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_Tuple.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdData_Utils.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdTask_Task.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaDebuger_Debuger.c
|
||||
SRC_FILES += pikascript-lib/PikaStdLib/PikaStdLib_MemChecker.c
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
class Debuger:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def set_trace(self):
|
||||
pass
|
||||
|
||||
def set_trace():
|
||||
pass
|
||||
|
||||
def set_break(module: str, pc_break: int):
|
||||
pass
|
||||
|
||||
def reset_break(module: str, pc_break: int):
|
||||
pass
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
class TinyObj:
|
||||
...
|
||||
|
||||
|
||||
class BaseObj(TinyObj):
|
||||
...
|
||||
|
||||
|
||||
class pointer:
|
||||
...
|
||||
|
||||
|
||||
class any:
|
||||
...
|
||||
|
||||
|
||||
class int64:
|
||||
...
|
||||
|
||||
|
||||
def printNoEnd(val: any): ...
|
||||
def abstractmethod(fn): ...
|
||||
def PIKA_C_MACRO_IF(fn): ...
|
||||
def PIKA_C_MACRO_IFDEF(fn): ...
|
||||
|
|
@ -1,170 +0,0 @@
|
|||
from PikaObj import *
|
||||
import builtins
|
||||
|
||||
|
||||
class Tuple:
|
||||
def __init__(self): ...
|
||||
|
||||
def get(self, i: int) -> any:
|
||||
"""get an arg by the index"""
|
||||
|
||||
def len(self) -> int:
|
||||
"""get the length of list"""
|
||||
|
||||
def __iter__(self) -> any:
|
||||
"""support for loop"""
|
||||
|
||||
def __next__(self) -> any:
|
||||
"""support for loop"""
|
||||
|
||||
def __getitem__(self, __key: any) -> any:
|
||||
"""support val = list[]"""
|
||||
|
||||
def __del__(self): ...
|
||||
def __str__(self) -> str: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __eq__(self, other: any) -> int:
|
||||
"""support tuple == tuple"""
|
||||
|
||||
def __add__(self, others: Tuple) -> Tuple:
|
||||
"""support tuple + tuple"""
|
||||
|
||||
def __mul__(self, n: int) -> Tuple:
|
||||
"""support tuple * int"""
|
||||
|
||||
class List(Tuple):
|
||||
def __init__(self): ...
|
||||
|
||||
def append(self, arg: any):
|
||||
"""add an arg after the end of list"""
|
||||
|
||||
def set(self, i: int, arg: any):
|
||||
"""set an arg by the index"""
|
||||
|
||||
def reverse(self):
|
||||
"""reverse the list"""
|
||||
|
||||
def pop(self, *index) -> any:
|
||||
"""pop the last element"""
|
||||
|
||||
def remove(self, val: any):
|
||||
"""remove the first element"""
|
||||
|
||||
def insert(self, i: int, arg: any):
|
||||
"""insert an arg before the index"""
|
||||
|
||||
def __setitem__(self, __key: any, __val: any):
|
||||
"""support list[] = val"""
|
||||
|
||||
def __str__(self) -> str: ...
|
||||
|
||||
def __add__(self, others: List) -> List:
|
||||
""" support list + list"""
|
||||
|
||||
def __mul__(self, n: int) -> Tuple:
|
||||
""" support list * int"""
|
||||
|
||||
class Dict:
|
||||
def __init__(self):
|
||||
""" get an arg by the key """
|
||||
|
||||
def get(self, key: str) -> any: ...
|
||||
|
||||
def set(self, key: str, arg: any):
|
||||
""" set an arg by the key """
|
||||
|
||||
def remove(self, key: str):
|
||||
""" remove an arg by the key """
|
||||
|
||||
def __iter__(self) -> any: ...
|
||||
def __next__(self) -> any: ...
|
||||
|
||||
def __setitem__(self, __key: any, __val: any):
|
||||
""" support dict[] = val """
|
||||
|
||||
def __getitem__(self, __key: any) -> any:
|
||||
""" support val = dict[] """
|
||||
|
||||
def __del__(self): ...
|
||||
def __str__(self) -> str: ...
|
||||
def keys(self) -> dict_keys: ...
|
||||
def items(self) -> dict_items: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __contains__(self, val: any) -> int:
|
||||
""" support val in dict """
|
||||
|
||||
def update(self, other: Dict):
|
||||
""" update dict """
|
||||
|
||||
def __eq__(self, other: any) -> int:
|
||||
""" support dict == dict """
|
||||
|
||||
|
||||
class dict_keys:
|
||||
def __iter__(self) -> any: ...
|
||||
def __next__(self) -> any: ...
|
||||
def __str__(self) -> str: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
|
||||
class dict_items:
|
||||
def __iter__(self) -> any: ...
|
||||
def __next__(self) -> any: ...
|
||||
def __str__(self) -> str: ...
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
|
||||
class String:
|
||||
def __init__(self, s: str): ...
|
||||
def set(self, s: str): ...
|
||||
def get(self) -> str: ...
|
||||
def __iter__(self) -> any: ...
|
||||
def __next__(self) -> any: ...
|
||||
|
||||
def __setitem__(self, __key: any, __val: any):
|
||||
""" support string[] = val """
|
||||
|
||||
def __getitem__(self, __key: any) -> any:
|
||||
""" support val = string[] """
|
||||
|
||||
def __str__(self) -> str:
|
||||
""" support str() """
|
||||
|
||||
def __len__(self) -> int: ...
|
||||
def encode(self, *encoding) -> bytes: ...
|
||||
def startswith(self, prefix: str) -> int: ...
|
||||
def endswith(self, suffix: str) -> int: ...
|
||||
def isdigit(self) -> int: ...
|
||||
def islower(self) -> int: ...
|
||||
def isalnum(self) -> int: ...
|
||||
def isalpha(self) -> int: ...
|
||||
def isspace(self) -> int: ...
|
||||
def split(self, *s) -> List: ...
|
||||
def replace(self, old: str, new: str) -> str: ...
|
||||
def strip(self, *chrs) -> str: ...
|
||||
def format(self, *vars) -> str: ...
|
||||
def join(self, val: any) -> str: ...
|
||||
def find(self, sub: str) -> int: ...
|
||||
|
||||
|
||||
class ByteArray(builtins.bytearray):
|
||||
pass
|
||||
|
||||
|
||||
class FILEIO:
|
||||
def init(self, path: str, mode: str) -> int: ...
|
||||
def read(self, *size) -> any: ...
|
||||
def write(self, s: any) -> int: ...
|
||||
def close(self): ...
|
||||
def seek(self, offset: int, *fromwhere) -> int: ...
|
||||
def tell(self) -> int: ...
|
||||
def readline(self) -> str: ...
|
||||
def readlines(self) -> List: ...
|
||||
def writelines(self, lines: List): ...
|
||||
|
||||
|
||||
class Utils:
|
||||
def int_to_bytes(self, val: int) -> bytes:
|
||||
""" convert a int to bytes """
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
from PikaObj import *
|
||||
|
||||
|
||||
class MemChecker:
|
||||
def max(self): ...
|
||||
def now(self): ...
|
||||
|
||||
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
|
||||
def getMax(self) -> float: ...
|
||||
|
||||
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
|
||||
def getNow(self) -> float: ...
|
||||
|
||||
@PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE")
|
||||
def resetMax(self): ...
|
||||
|
||||
|
||||
class REPL:
|
||||
def setEcho(self, echo: bool): ...
|
||||
|
||||
|
||||
class SysObj:
|
||||
pass
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
import PikaStdData
|
||||
import PikaStdLib
|
||||
|
||||
|
||||
class Task(PikaStdLib.SysObj):
|
||||
calls = PikaStdData.List()
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
# regist a function to be called always
|
||||
def call_always(self, fun_todo: any):
|
||||
pass
|
||||
|
||||
# regist a function to be called when fun_when() return 'True'
|
||||
def call_when(self, fun_todo: any, fun_when: any):
|
||||
pass
|
||||
|
||||
# regist a function to be called periodically
|
||||
def call_period_ms(self, fun_todo: any, period_ms: int):
|
||||
pass
|
||||
|
||||
# run all registed function once
|
||||
def run_once(self):
|
||||
pass
|
||||
|
||||
# run all registed function forever
|
||||
def run_forever(self):
|
||||
pass
|
||||
|
||||
# run all registed function until time is up
|
||||
def run_until_ms(self, until_ms: int):
|
||||
pass
|
||||
|
||||
# need be overried to supply the system tick
|
||||
def platformGetTick(self):
|
||||
pass
|
||||
|
|
@ -1,188 +0,0 @@
|
|||
# PikaPython 移植到 XiUOS
|
||||
|
||||
本文档描述了如何将 PikaPython 嵌入式 Python 解释器移植到 XiUOS 操作系统。
|
||||
|
||||
## 概述
|
||||
|
||||
PikaPython 是一个专为嵌入式系统设计的轻量级 Python 解释器,具有以下特点:
|
||||
- 内存占用小(ARM 内核至少 64KB Flash + 8KB RAM)
|
||||
- 支持标准 Python 语法
|
||||
- 可扩展的硬件抽象层
|
||||
- 支持预编译脚本
|
||||
|
||||
## 移植架构
|
||||
|
||||
```
|
||||
APP_Framework/lib/pikapython/
|
||||
├── pikascript-core/ # PikaPython 核心解释器
|
||||
│ ├── PikaVM.c # 虚拟机
|
||||
│ ├── PikaObj.c # 对象系统
|
||||
│ ├── PikaParser.c # 解析器
|
||||
│ ├── PikaCompiler.c # 编译器
|
||||
│ ├── PikaPlatform.c # 默认平台实现(使用标准C库)
|
||||
│ └── ... # 其他核心文件
|
||||
├── pikascript-api/ # 预编译的Python API
|
||||
│ ├── pikaScript.c # 主脚本执行器
|
||||
│ ├── pikaScript.h # 主脚本头文件
|
||||
│ ├── __pikaBinding.c # Python绑定
|
||||
│ └── __asset_pikaModules_py_a.c # 预编译的Python模块
|
||||
├── pikascript-lib/ # PikaPython 标准库
|
||||
│ └── PikaStdLib/ # 标准库实现
|
||||
├── main.py # Python测试脚本(已预编译)
|
||||
├── Kconfig # 配置选项
|
||||
├── Makefile # 构建文件
|
||||
└── README.md # 说明文档
|
||||
```
|
||||
|
||||
## 为什么不需要额外的平台适配层?
|
||||
|
||||
PikaPython**不依赖操作系统和文件系统**,只依赖标准C库,并且:
|
||||
|
||||
### 1. **内置默认平台实现**
|
||||
- `pikascript-core/PikaPlatform.c` 提供了完整的默认平台实现
|
||||
- 直接使用标准C库函数(malloc、printf、strcpy等)
|
||||
- 无需额外的平台适配代码
|
||||
|
||||
### 2. **预编译的优势**
|
||||
- Python脚本预编译为C代码
|
||||
- 无需运行时解释器
|
||||
- 直接调用预编译的C函数
|
||||
|
||||
### 3. **最小化依赖**
|
||||
- 只依赖标准C库
|
||||
- 无需操作系统特定代码
|
||||
- 跨平台兼容
|
||||
|
||||
## 配置选项
|
||||
|
||||
### 基本配置
|
||||
- `CONFIG_LIB_USING_PIKAPYTHON`: 启用 PikaPython 支持
|
||||
|
||||
**注意**: PikaPython 使用其内部配置系统,无需额外的配置选项。
|
||||
|
||||
## 使用方法
|
||||
|
||||
### 1. 配置编译选项
|
||||
|
||||
在项目配置中启用 PikaPython:
|
||||
```bash
|
||||
# 使用 menuconfig 或直接编辑 .config 文件
|
||||
CONFIG_LIB_USING_PIKAPYTHON=y
|
||||
```
|
||||
|
||||
### 2. 在应用程序中使用
|
||||
|
||||
```c
|
||||
#ifdef CONFIG_LIB_USING_PIKAPYTHON
|
||||
#include "pikaScript.h"
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
#ifdef CONFIG_LIB_USING_PIKAPYTHON
|
||||
// 初始化并运行 PikaPython(自动执行预编译的main.py)
|
||||
PikaObj* pikaMain = pikaPythonInit();
|
||||
|
||||
if (pikaMain != NULL) {
|
||||
printf("PikaPython test completed successfully!\n");
|
||||
} else {
|
||||
printf("PikaPython initialization failed!\n");
|
||||
}
|
||||
#else
|
||||
printf("PikaPython is disabled in configuration.\n");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
**注意**: `CONFIG_LIB_USING_PIKAPYTHON` 宏是通过编译器命令行传递的,无需包含额外的头文件。
|
||||
|
||||
### 3. 测试脚本
|
||||
|
||||
运行测试:
|
||||
```bash
|
||||
# 编译并运行测试程序
|
||||
make
|
||||
./test_pikapython
|
||||
```
|
||||
|
||||
## 移植步骤详解
|
||||
|
||||
### 第一步:获取 PikaPython 源码
|
||||
|
||||
1. 从 [PikaPython GitHub](https://github.com/pikastech/pikapython) 下载源码
|
||||
2. 将 `pikascript-core` 和 `PikaStdLib` 复制到 `src/` 目录
|
||||
|
||||
### 第二步:实现平台适配
|
||||
|
||||
1. **内存管理**:实现 `pika_malloc()`, `pika_free()`, `pika_realloc()`
|
||||
2. **字符串操作**:实现 `pika_strdup()`, `pika_strcmp()` 等
|
||||
3. **打印函数**:实现 `pika_printf()`, `pika_sprintf()` 等
|
||||
4. **时间函数**:实现 `pika_get_tick()`, `pika_delay_ms()`
|
||||
|
||||
### 第三步:硬件抽象层适配
|
||||
|
||||
1. **GPIO 操作**:`pika_hal_gpio_init()`, `pika_hal_gpio_write()`, `pika_hal_gpio_read()`
|
||||
2. **UART 操作**:`pika_hal_uart_init()`, `pika_hal_uart_write()`, `pika_hal_uart_read()`
|
||||
3. **I2C 操作**:`pika_hal_i2c_init()`, `pika_hal_i2c_write()`, `pika_hal_i2c_read()`
|
||||
4. **SPI 操作**:`pika_hal_spi_init()`, `pika_hal_spi_write()`, `pika_hal_spi_read()`
|
||||
5. **ADC 操作**:`pika_hal_adc_init()`, `pika_hal_adc_read()`
|
||||
6. **PWM 操作**:`pika_hal_pwm_init()`, `pika_hal_pwm_set_duty()`
|
||||
|
||||
### 第四步:集成到构建系统
|
||||
|
||||
1. 更新 `APP_Framework/lib/Kconfig` 包含 PikaPython 配置
|
||||
2. 更新 `APP_Framework/lib/Makefile` 包含 PikaPython 源码
|
||||
3. 创建应用程序测试代码
|
||||
|
||||
## 注意事项
|
||||
|
||||
### 内存要求
|
||||
- **ARM 内核**:至少 64KB Flash + 8KB RAM
|
||||
- **RISC-V 内核**:至少 128KB Flash + 8KB RAM
|
||||
- 确保编译器支持 C99 标准
|
||||
|
||||
### 依赖关系
|
||||
- 需要完整的标准 C 库(libc)支持
|
||||
- 需要基本的输入输出功能
|
||||
- 可选的文件系统支持
|
||||
|
||||
### 调试建议
|
||||
1. 启用 `CONFIG_PIKAPYTHON_ENABLE_DEBUG` 查看调试信息
|
||||
2. 使用串口输出查看 Python 脚本执行结果
|
||||
3. 检查内存使用情况,避免栈溢出
|
||||
|
||||
## 扩展功能
|
||||
|
||||
### 文件系统支持
|
||||
如果启用文件系统支持,可以:
|
||||
- 从文件加载 Python 脚本
|
||||
- 保存 Python 脚本执行结果
|
||||
- 支持模块化 Python 代码
|
||||
|
||||
### 硬件访问
|
||||
如果启用硬件访问,可以:
|
||||
- 在 Python 中控制 GPIO
|
||||
- 使用 UART、I2C、SPI 等外设
|
||||
- 读取 ADC 值、控制 PWM 输出
|
||||
|
||||
## 故障排除
|
||||
|
||||
### 常见问题
|
||||
1. **内存不足**:检查系统可用内存
|
||||
2. **栈溢出**:检查系统栈配置
|
||||
3. **编译错误**:检查 C99 标准支持
|
||||
4. **运行时错误**:启用调试输出查看详细信息
|
||||
|
||||
### 调试技巧
|
||||
1. 使用 `pika_printf()` 输出调试信息
|
||||
2. 检查内存分配是否成功
|
||||
3. 验证平台接口实现是否正确
|
||||
4. 测试基本的 Python 语法支持
|
||||
|
||||
## 参考资料
|
||||
|
||||
- [PikaPython 官方文档](https://pikapython.com/)
|
||||
- [PikaPython GitHub 仓库](https://github.com/pikastech/pikapython)
|
||||
- [XiUOS 官方文档](https://www.xiuos.com/)
|
||||
|
|
@ -1,410 +0,0 @@
|
|||
from PikaObj import *
|
||||
|
||||
|
||||
def int(arg: any, *base) -> any: ...
|
||||
|
||||
|
||||
def bool(arg: any) -> bool: ...
|
||||
|
||||
|
||||
def float(arg: any) -> float: ...
|
||||
|
||||
|
||||
def str(arg: any) -> str: ...
|
||||
|
||||
|
||||
def iter(arg: any) -> any: ...
|
||||
|
||||
|
||||
def range(*ax) -> any: ...
|
||||
|
||||
|
||||
def print(*val, **ops): ...
|
||||
|
||||
|
||||
def __setitem__(obj: any, key: any, val: any) -> any: ...
|
||||
|
||||
|
||||
def __getitem__(obj: any, key: any) -> any: ...
|
||||
|
||||
|
||||
def type(arg: any) -> any: ...
|
||||
|
||||
|
||||
def isinstance(object: any, classinfo: any) -> bool: ...
|
||||
|
||||
|
||||
def len(arg: any) -> int: ...
|
||||
|
||||
|
||||
def list(*val) -> any: ...
|
||||
|
||||
|
||||
def dict(*val) -> any: ...
|
||||
|
||||
|
||||
def tuple(*val) -> any: ...
|
||||
|
||||
|
||||
def hex(val: int) -> str: ...
|
||||
|
||||
|
||||
def ord(val: str) -> int: ...
|
||||
|
||||
|
||||
def chr(val: int) -> str: ...
|
||||
|
||||
|
||||
def bytes(val: any) -> bytes: ...
|
||||
|
||||
|
||||
def cformat(fmt: str, *var) -> str: ...
|
||||
|
||||
|
||||
def id(obj: any) -> int: ...
|
||||
|
||||
|
||||
def open(path: str, mode: str) -> object: ...
|
||||
|
||||
|
||||
def dir(obj: any) -> list: ...
|
||||
|
||||
|
||||
def exec(code: str): ...
|
||||
|
||||
|
||||
def eval(code: str) -> any: ...
|
||||
|
||||
|
||||
def getattr(obj: object, name: str) -> any: ...
|
||||
|
||||
|
||||
def setattr(obj: object, name: str, val: any): ...
|
||||
|
||||
|
||||
def hasattr(obj: object, name: str) -> int: ...
|
||||
|
||||
|
||||
def exit(): ...
|
||||
|
||||
|
||||
def input(*info) -> str: ...
|
||||
|
||||
|
||||
def abs(val: any) -> any: ...
|
||||
|
||||
|
||||
def max(*val) -> any: ...
|
||||
|
||||
|
||||
def min(*val) -> any: ...
|
||||
|
||||
|
||||
def help(name: str): ...
|
||||
|
||||
|
||||
def reboot(): ...
|
||||
|
||||
|
||||
def clear(): ...
|
||||
|
||||
|
||||
def gcdump(): ...
|
||||
|
||||
|
||||
class RangeObj:
|
||||
def __next__(self) -> any: ...
|
||||
|
||||
|
||||
class StringObj:
|
||||
def __next__(self) -> any: ...
|
||||
|
||||
|
||||
class object:
|
||||
pass
|
||||
|
||||
|
||||
class bytearray:
|
||||
def __init__(self, bytes: any):
|
||||
""" convert a bytes to ByteArray """
|
||||
|
||||
def __iter__(self) -> any:
|
||||
""" support for loop """
|
||||
|
||||
def __next__(self) -> any:
|
||||
""" support for loop """
|
||||
|
||||
def __getitem__(self, __key: int) -> int:
|
||||
""" support [] index """
|
||||
|
||||
def __setitem__(self, __key: int, __val: int): ...
|
||||
|
||||
def __str__(self) -> str: ...
|
||||
|
||||
def decode(self) -> str: ...
|
||||
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __contains__(self, others: any) -> int: ...
|
||||
|
||||
def split(self, *vars) -> list: ...
|
||||
|
||||
|
||||
class BaseException:
|
||||
pass
|
||||
|
||||
|
||||
class Exception(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
class SystemExit(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
class KeyboardInterrupt(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
class GeneratorExit(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
class Exception(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
class StopIteration(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class StopAsyncIteration(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ArithmeticError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class FloatingPointError(ArithmeticError):
|
||||
pass
|
||||
|
||||
|
||||
class OverflowError(ArithmeticError):
|
||||
pass
|
||||
|
||||
|
||||
class ZeroDivisionError(ArithmeticError):
|
||||
pass
|
||||
|
||||
|
||||
class AssertionError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AttributeError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class BufferError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class EOFError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ImportError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ModuleNotFoundError(ImportError):
|
||||
pass
|
||||
|
||||
|
||||
class LookupError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class IndexError(LookupError):
|
||||
pass
|
||||
|
||||
|
||||
class KeyError(LookupError):
|
||||
pass
|
||||
|
||||
|
||||
class MemoryError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class NameError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UnboundLocalError(NameError):
|
||||
pass
|
||||
|
||||
|
||||
class OSError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class BlockingIOError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class ChildProcessError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class ConnectionError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class BrokenPipeError(ConnectionError):
|
||||
pass
|
||||
|
||||
|
||||
class ConnectionAbortedError(ConnectionError):
|
||||
pass
|
||||
|
||||
|
||||
class ConnectionRefusedError(ConnectionError):
|
||||
pass
|
||||
|
||||
|
||||
class ConnectionResetError(ConnectionError):
|
||||
pass
|
||||
|
||||
|
||||
class FileExistsError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class FileNotFoundError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class InterruptedError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class IsADirectoryError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class NotADirectoryError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class PermissionError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class ProcessLookupError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class TimeoutError(OSError):
|
||||
pass
|
||||
|
||||
|
||||
class ReferenceError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class RuntimeError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class NotImplementedError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class RecursionError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class SyntaxError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class IndentationError(SyntaxError):
|
||||
pass
|
||||
|
||||
|
||||
class TabError(IndentationError):
|
||||
pass
|
||||
|
||||
|
||||
class SystemError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class TypeError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ValueError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UnicodeError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
class UnicodeDecodeError(UnicodeError):
|
||||
pass
|
||||
|
||||
|
||||
class UnicodeEncodeError(UnicodeError):
|
||||
pass
|
||||
|
||||
|
||||
class UnicodeTranslateError(UnicodeError):
|
||||
pass
|
||||
|
||||
|
||||
class Warning(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class DeprecationWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class PendingDeprecationWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class RuntimeWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class SyntaxWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class UserWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class FutureWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class ImportWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class UnicodeWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class BytesWarning(Warning):
|
||||
pass
|
||||
|
||||
|
||||
class ResourceWarning(Warning):
|
||||
pass
|
||||
|
|
@ -1 +0,0 @@
|
|||
print('hello PikaPython!')
|
||||
Binary file not shown.
|
|
@ -1,25 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaDebug__H
|
||||
#define __PikaDebug__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaDebug(Args *args);
|
||||
|
||||
Arg* PikaDebug_Debuger(PikaObj *self);
|
||||
void PikaDebug_reset_break(PikaObj *self, char* module, int pc_break);
|
||||
void PikaDebug_set_break(PikaObj *self, char* module, int pc_break);
|
||||
void PikaDebug_set_trace(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaDebug_Debuger__H
|
||||
#define __PikaDebug_Debuger__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaDebug_Debuger(Args *args);
|
||||
|
||||
void PikaDebug_Debuger___init__(PikaObj *self);
|
||||
void PikaDebug_Debuger_set_trace(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaMain__H
|
||||
#define __PikaMain__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaMain(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData__H
|
||||
#define __PikaStdData__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData(Args *args);
|
||||
|
||||
Arg* PikaStdData_ByteArray(PikaObj *self);
|
||||
Arg* PikaStdData_Dict(PikaObj *self);
|
||||
Arg* PikaStdData_FILEIO(PikaObj *self);
|
||||
Arg* PikaStdData_List(PikaObj *self);
|
||||
Arg* PikaStdData_String(PikaObj *self);
|
||||
Arg* PikaStdData_Tuple(PikaObj *self);
|
||||
Arg* PikaStdData_Utils(PikaObj *self);
|
||||
Arg* PikaStdData_dict_items(PikaObj *self);
|
||||
Arg* PikaStdData_dict_keys(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_ByteArray__H
|
||||
#define __PikaStdData_ByteArray__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_ByteArray(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_Dict__H
|
||||
#define __PikaStdData_Dict__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_Dict(Args *args);
|
||||
|
||||
int PikaStdData_Dict___contains__(PikaObj *self, Arg* val);
|
||||
void PikaStdData_Dict___del__(PikaObj *self);
|
||||
int PikaStdData_Dict___eq__(PikaObj *self, Arg* other);
|
||||
Arg* PikaStdData_Dict___getitem__(PikaObj *self, Arg* __key);
|
||||
void PikaStdData_Dict___init__(PikaObj *self);
|
||||
Arg* PikaStdData_Dict___iter__(PikaObj *self);
|
||||
int PikaStdData_Dict___len__(PikaObj *self);
|
||||
Arg* PikaStdData_Dict___next__(PikaObj *self);
|
||||
void PikaStdData_Dict___setitem__(PikaObj *self, Arg* __key, Arg* __val);
|
||||
char* PikaStdData_Dict___str__(PikaObj *self);
|
||||
Arg* PikaStdData_Dict_get(PikaObj *self, char* key);
|
||||
PikaObj* PikaStdData_Dict_items(PikaObj *self);
|
||||
PikaObj* PikaStdData_Dict_keys(PikaObj *self);
|
||||
void PikaStdData_Dict_remove(PikaObj *self, char* key);
|
||||
void PikaStdData_Dict_set(PikaObj *self, char* key, Arg* arg);
|
||||
void PikaStdData_Dict_update(PikaObj *self, PikaObj* other);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_FILEIO__H
|
||||
#define __PikaStdData_FILEIO__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_FILEIO(Args *args);
|
||||
|
||||
void PikaStdData_FILEIO_close(PikaObj *self);
|
||||
int PikaStdData_FILEIO_init(PikaObj *self, char* path, char* mode);
|
||||
Arg* PikaStdData_FILEIO_read(PikaObj *self, PikaTuple* size);
|
||||
char* PikaStdData_FILEIO_readline(PikaObj *self);
|
||||
PikaObj* PikaStdData_FILEIO_readlines(PikaObj *self);
|
||||
int PikaStdData_FILEIO_seek(PikaObj *self, int offset, PikaTuple* fromwhere);
|
||||
int PikaStdData_FILEIO_tell(PikaObj *self);
|
||||
int PikaStdData_FILEIO_write(PikaObj *self, Arg* s);
|
||||
void PikaStdData_FILEIO_writelines(PikaObj *self, PikaObj* lines);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_List__H
|
||||
#define __PikaStdData_List__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_List(Args *args);
|
||||
|
||||
PikaObj* PikaStdData_List___add__(PikaObj *self, PikaObj* others);
|
||||
void PikaStdData_List___init__(PikaObj *self);
|
||||
PikaObj* PikaStdData_List___mul__(PikaObj *self, int n);
|
||||
void PikaStdData_List___setitem__(PikaObj *self, Arg* __key, Arg* __val);
|
||||
char* PikaStdData_List___str__(PikaObj *self);
|
||||
void PikaStdData_List_append(PikaObj *self, Arg* arg);
|
||||
void PikaStdData_List_insert(PikaObj *self, int i, Arg* arg);
|
||||
Arg* PikaStdData_List_pop(PikaObj *self, PikaTuple* index);
|
||||
void PikaStdData_List_remove(PikaObj *self, Arg* val);
|
||||
void PikaStdData_List_reverse(PikaObj *self);
|
||||
void PikaStdData_List_set(PikaObj *self, int i, Arg* arg);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_String__H
|
||||
#define __PikaStdData_String__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_String(Args *args);
|
||||
|
||||
Arg* PikaStdData_String___getitem__(PikaObj *self, Arg* __key);
|
||||
void PikaStdData_String___init__(PikaObj *self, char* s);
|
||||
Arg* PikaStdData_String___iter__(PikaObj *self);
|
||||
int PikaStdData_String___len__(PikaObj *self);
|
||||
Arg* PikaStdData_String___next__(PikaObj *self);
|
||||
void PikaStdData_String___setitem__(PikaObj *self, Arg* __key, Arg* __val);
|
||||
char* PikaStdData_String___str__(PikaObj *self);
|
||||
Arg* PikaStdData_String_encode(PikaObj *self, PikaTuple* encoding);
|
||||
int PikaStdData_String_endswith(PikaObj *self, char* suffix);
|
||||
int PikaStdData_String_find(PikaObj *self, char* sub);
|
||||
char* PikaStdData_String_format(PikaObj *self, PikaTuple* vars);
|
||||
char* PikaStdData_String_get(PikaObj *self);
|
||||
int PikaStdData_String_isalnum(PikaObj *self);
|
||||
int PikaStdData_String_isalpha(PikaObj *self);
|
||||
int PikaStdData_String_isdigit(PikaObj *self);
|
||||
int PikaStdData_String_islower(PikaObj *self);
|
||||
int PikaStdData_String_isspace(PikaObj *self);
|
||||
char* PikaStdData_String_join(PikaObj *self, Arg* val);
|
||||
char* PikaStdData_String_replace(PikaObj *self, char* old, char* new);
|
||||
void PikaStdData_String_set(PikaObj *self, char* s);
|
||||
PikaObj* PikaStdData_String_split(PikaObj *self, PikaTuple* s);
|
||||
int PikaStdData_String_startswith(PikaObj *self, char* prefix);
|
||||
char* PikaStdData_String_strip(PikaObj *self, PikaTuple* chrs);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_Tuple__H
|
||||
#define __PikaStdData_Tuple__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_Tuple(Args *args);
|
||||
|
||||
PikaObj* PikaStdData_Tuple___add__(PikaObj *self, PikaObj* others);
|
||||
void PikaStdData_Tuple___del__(PikaObj *self);
|
||||
int PikaStdData_Tuple___eq__(PikaObj *self, Arg* other);
|
||||
Arg* PikaStdData_Tuple___getitem__(PikaObj *self, Arg* __key);
|
||||
void PikaStdData_Tuple___init__(PikaObj *self);
|
||||
Arg* PikaStdData_Tuple___iter__(PikaObj *self);
|
||||
int PikaStdData_Tuple___len__(PikaObj *self);
|
||||
PikaObj* PikaStdData_Tuple___mul__(PikaObj *self, int n);
|
||||
Arg* PikaStdData_Tuple___next__(PikaObj *self);
|
||||
char* PikaStdData_Tuple___str__(PikaObj *self);
|
||||
Arg* PikaStdData_Tuple_get(PikaObj *self, int i);
|
||||
int PikaStdData_Tuple_len(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_Utils__H
|
||||
#define __PikaStdData_Utils__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_Utils(Args *args);
|
||||
|
||||
Arg* PikaStdData_Utils_int_to_bytes(PikaObj *self, int val);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_dict_items__H
|
||||
#define __PikaStdData_dict_items__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_dict_items(Args *args);
|
||||
|
||||
Arg* PikaStdData_dict_items___iter__(PikaObj *self);
|
||||
int PikaStdData_dict_items___len__(PikaObj *self);
|
||||
Arg* PikaStdData_dict_items___next__(PikaObj *self);
|
||||
char* PikaStdData_dict_items___str__(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdData_dict_keys__H
|
||||
#define __PikaStdData_dict_keys__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdData_dict_keys(Args *args);
|
||||
|
||||
Arg* PikaStdData_dict_keys___iter__(PikaObj *self);
|
||||
int PikaStdData_dict_keys___len__(PikaObj *self);
|
||||
Arg* PikaStdData_dict_keys___next__(PikaObj *self);
|
||||
char* PikaStdData_dict_keys___str__(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdLib__H
|
||||
#define __PikaStdLib__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdLib(Args *args);
|
||||
|
||||
Arg* PikaStdLib_MemChecker(PikaObj *self);
|
||||
Arg* PikaStdLib_REPL(PikaObj *self);
|
||||
Arg* PikaStdLib_SysObj(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdLib_MemChecker__H
|
||||
#define __PikaStdLib_MemChecker__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdLib_MemChecker(Args *args);
|
||||
|
||||
pika_float PikaStdLib_MemChecker_getMax(PikaObj *self);
|
||||
pika_float PikaStdLib_MemChecker_getNow(PikaObj *self);
|
||||
void PikaStdLib_MemChecker_max(PikaObj *self);
|
||||
void PikaStdLib_MemChecker_now(PikaObj *self);
|
||||
void PikaStdLib_MemChecker_resetMax(PikaObj *self);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdLib_REPL__H
|
||||
#define __PikaStdLib_REPL__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdLib_REPL(Args *args);
|
||||
|
||||
void PikaStdLib_REPL_setEcho(PikaObj *self, pika_bool echo);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdLib_SysObj__H
|
||||
#define __PikaStdLib_SysObj__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdLib_SysObj(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdTask__H
|
||||
#define __PikaStdTask__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdTask(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __PikaStdTask_Task__H
|
||||
#define __PikaStdTask_Task__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_PikaStdTask_Task(Args *args);
|
||||
|
||||
void PikaStdTask_Task___init__(PikaObj *self);
|
||||
void PikaStdTask_Task_call_always(PikaObj *self, Arg* fun_todo);
|
||||
void PikaStdTask_Task_call_period_ms(PikaObj *self, Arg* fun_todo, int period_ms);
|
||||
void PikaStdTask_Task_call_when(PikaObj *self, Arg* fun_todo, Arg* fun_when);
|
||||
void PikaStdTask_Task_platformGetTick(PikaObj *self);
|
||||
void PikaStdTask_Task_run_forever(PikaObj *self);
|
||||
void PikaStdTask_Task_run_once(PikaObj *self);
|
||||
void PikaStdTask_Task_run_until_ms(PikaObj *self, int until_ms);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
#include "../pikascript-core/PikaPlatform.h"
|
||||
/* warning: auto generated file, please do not modify */
|
||||
PIKA_BYTECODE_ALIGN const unsigned char pikaModules_py_a[] = {
|
||||
0x0f, 0x70, 0x79, 0x61, 0xac, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
|
||||
0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
|
||||
0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x13, 0x00, 0x19, 0x00, 0x00, 0x00,
|
||||
0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x50, 0x69, 0x6b, 0x61, 0x50,
|
||||
0x79, 0x74, 0x68, 0x6f, 0x6e, 0x21, 0x00, 0x70, 0x72, 0x69, 0x6e, 0x74,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,126 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins__H
|
||||
#define __builtins__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins(Args *args);
|
||||
|
||||
Arg* builtins_ArithmeticError(PikaObj *self);
|
||||
Arg* builtins_AssertionError(PikaObj *self);
|
||||
Arg* builtins_AttributeError(PikaObj *self);
|
||||
Arg* builtins_BaseException(PikaObj *self);
|
||||
Arg* builtins_BlockingIOError(PikaObj *self);
|
||||
Arg* builtins_BrokenPipeError(PikaObj *self);
|
||||
Arg* builtins_BufferError(PikaObj *self);
|
||||
Arg* builtins_BytesWarning(PikaObj *self);
|
||||
Arg* builtins_ChildProcessError(PikaObj *self);
|
||||
Arg* builtins_ConnectionAbortedError(PikaObj *self);
|
||||
Arg* builtins_ConnectionError(PikaObj *self);
|
||||
Arg* builtins_ConnectionRefusedError(PikaObj *self);
|
||||
Arg* builtins_ConnectionResetError(PikaObj *self);
|
||||
Arg* builtins_DeprecationWarning(PikaObj *self);
|
||||
Arg* builtins_EOFError(PikaObj *self);
|
||||
Arg* builtins_Exception(PikaObj *self);
|
||||
Arg* builtins_FileExistsError(PikaObj *self);
|
||||
Arg* builtins_FileNotFoundError(PikaObj *self);
|
||||
Arg* builtins_FloatingPointError(PikaObj *self);
|
||||
Arg* builtins_FutureWarning(PikaObj *self);
|
||||
Arg* builtins_GeneratorExit(PikaObj *self);
|
||||
Arg* builtins_ImportError(PikaObj *self);
|
||||
Arg* builtins_ImportWarning(PikaObj *self);
|
||||
Arg* builtins_IndentationError(PikaObj *self);
|
||||
Arg* builtins_IndexError(PikaObj *self);
|
||||
Arg* builtins_InterruptedError(PikaObj *self);
|
||||
Arg* builtins_IsADirectoryError(PikaObj *self);
|
||||
Arg* builtins_KeyError(PikaObj *self);
|
||||
Arg* builtins_KeyboardInterrupt(PikaObj *self);
|
||||
Arg* builtins_LookupError(PikaObj *self);
|
||||
Arg* builtins_MemoryError(PikaObj *self);
|
||||
Arg* builtins_ModuleNotFoundError(PikaObj *self);
|
||||
Arg* builtins_NameError(PikaObj *self);
|
||||
Arg* builtins_NotADirectoryError(PikaObj *self);
|
||||
Arg* builtins_NotImplementedError(PikaObj *self);
|
||||
Arg* builtins_OSError(PikaObj *self);
|
||||
Arg* builtins_OverflowError(PikaObj *self);
|
||||
Arg* builtins_PendingDeprecationWarning(PikaObj *self);
|
||||
Arg* builtins_PermissionError(PikaObj *self);
|
||||
Arg* builtins_ProcessLookupError(PikaObj *self);
|
||||
Arg* builtins_RangeObj(PikaObj *self);
|
||||
Arg* builtins_RecursionError(PikaObj *self);
|
||||
Arg* builtins_ReferenceError(PikaObj *self);
|
||||
Arg* builtins_ResourceWarning(PikaObj *self);
|
||||
Arg* builtins_RuntimeError(PikaObj *self);
|
||||
Arg* builtins_RuntimeWarning(PikaObj *self);
|
||||
Arg* builtins_StopAsyncIteration(PikaObj *self);
|
||||
Arg* builtins_StopIteration(PikaObj *self);
|
||||
Arg* builtins_StringObj(PikaObj *self);
|
||||
Arg* builtins_SyntaxError(PikaObj *self);
|
||||
Arg* builtins_SyntaxWarning(PikaObj *self);
|
||||
Arg* builtins_SystemError(PikaObj *self);
|
||||
Arg* builtins_SystemExit(PikaObj *self);
|
||||
Arg* builtins_TabError(PikaObj *self);
|
||||
Arg* builtins_TimeoutError(PikaObj *self);
|
||||
Arg* builtins_TypeError(PikaObj *self);
|
||||
Arg* builtins_UnboundLocalError(PikaObj *self);
|
||||
Arg* builtins_UnicodeDecodeError(PikaObj *self);
|
||||
Arg* builtins_UnicodeEncodeError(PikaObj *self);
|
||||
Arg* builtins_UnicodeError(PikaObj *self);
|
||||
Arg* builtins_UnicodeTranslateError(PikaObj *self);
|
||||
Arg* builtins_UnicodeWarning(PikaObj *self);
|
||||
Arg* builtins_UserWarning(PikaObj *self);
|
||||
Arg* builtins_ValueError(PikaObj *self);
|
||||
Arg* builtins_Warning(PikaObj *self);
|
||||
Arg* builtins_ZeroDivisionError(PikaObj *self);
|
||||
Arg* builtins___getitem__(PikaObj *self, Arg* obj, Arg* key);
|
||||
Arg* builtins___setitem__(PikaObj *self, Arg* obj, Arg* key, Arg* val);
|
||||
Arg* builtins_abs(PikaObj *self, Arg* val);
|
||||
pika_bool builtins_bool(PikaObj *self, Arg* arg);
|
||||
Arg* builtins_bytearray(PikaObj *self);
|
||||
Arg* builtins_bytes(PikaObj *self, Arg* val);
|
||||
char* builtins_cformat(PikaObj *self, char* fmt, PikaTuple* var);
|
||||
char* builtins_chr(PikaObj *self, int val);
|
||||
void builtins_clear(PikaObj *self);
|
||||
Arg* builtins_dict(PikaObj *self, PikaTuple* val);
|
||||
PikaObj* builtins_dir(PikaObj *self, Arg* obj);
|
||||
Arg* builtins_eval(PikaObj *self, char* code);
|
||||
void builtins_exec(PikaObj *self, char* code);
|
||||
void builtins_exit(PikaObj *self);
|
||||
pika_float builtins_float(PikaObj *self, Arg* arg);
|
||||
void builtins_gcdump(PikaObj *self);
|
||||
Arg* builtins_getattr(PikaObj *self, PikaObj* obj, char* name);
|
||||
int builtins_hasattr(PikaObj *self, PikaObj* obj, char* name);
|
||||
void builtins_help(PikaObj *self, char* name);
|
||||
char* builtins_hex(PikaObj *self, int val);
|
||||
int builtins_id(PikaObj *self, Arg* obj);
|
||||
char* builtins_input(PikaObj *self, PikaTuple* info);
|
||||
Arg* builtins_int(PikaObj *self, Arg* arg, PikaTuple* base);
|
||||
pika_bool builtins_isinstance(PikaObj *self, Arg* object, Arg* classinfo);
|
||||
Arg* builtins_iter(PikaObj *self, Arg* arg);
|
||||
int builtins_len(PikaObj *self, Arg* arg);
|
||||
Arg* builtins_list(PikaObj *self, PikaTuple* val);
|
||||
Arg* builtins_max(PikaObj *self, PikaTuple* val);
|
||||
Arg* builtins_min(PikaObj *self, PikaTuple* val);
|
||||
Arg* builtins_object(PikaObj *self);
|
||||
PikaObj* builtins_open(PikaObj *self, char* path, char* mode);
|
||||
int builtins_ord(PikaObj *self, char* val);
|
||||
void builtins_print(PikaObj *self, PikaTuple* val, PikaDict* ops);
|
||||
Arg* builtins_range(PikaObj *self, PikaTuple* ax);
|
||||
void builtins_reboot(PikaObj *self);
|
||||
void builtins_setattr(PikaObj *self, PikaObj* obj, char* name, Arg* val);
|
||||
char* builtins_str(PikaObj *self, Arg* arg);
|
||||
Arg* builtins_tuple(PikaObj *self, PikaTuple* val);
|
||||
Arg* builtins_type(PikaObj *self, Arg* arg);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_ArithmeticError__H
|
||||
#define __builtins_ArithmeticError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_ArithmeticError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_AssertionError__H
|
||||
#define __builtins_AssertionError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_AssertionError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_AttributeError__H
|
||||
#define __builtins_AttributeError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_AttributeError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_BaseException__H
|
||||
#define __builtins_BaseException__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_BaseException(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_BlockingIOError__H
|
||||
#define __builtins_BlockingIOError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_BlockingIOError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_BrokenPipeError__H
|
||||
#define __builtins_BrokenPipeError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_BrokenPipeError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_BufferError__H
|
||||
#define __builtins_BufferError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_BufferError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_BytesWarning__H
|
||||
#define __builtins_BytesWarning__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_BytesWarning(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_ChildProcessError__H
|
||||
#define __builtins_ChildProcessError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_ChildProcessError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_ConnectionAbortedError__H
|
||||
#define __builtins_ConnectionAbortedError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_ConnectionAbortedError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_ConnectionError__H
|
||||
#define __builtins_ConnectionError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_ConnectionError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_ConnectionRefusedError__H
|
||||
#define __builtins_ConnectionRefusedError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_ConnectionRefusedError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_ConnectionResetError__H
|
||||
#define __builtins_ConnectionResetError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_ConnectionResetError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_DeprecationWarning__H
|
||||
#define __builtins_DeprecationWarning__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_DeprecationWarning(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_EOFError__H
|
||||
#define __builtins_EOFError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_EOFError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_Exception__H
|
||||
#define __builtins_Exception__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_Exception(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_FileExistsError__H
|
||||
#define __builtins_FileExistsError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_FileExistsError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_FileNotFoundError__H
|
||||
#define __builtins_FileNotFoundError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_FileNotFoundError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_FloatingPointError__H
|
||||
#define __builtins_FloatingPointError__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_FloatingPointError(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_FutureWarning__H
|
||||
#define __builtins_FutureWarning__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_FutureWarning(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* [Warning!] This file is auto-generated by pika compiler.
|
||||
* Do not edit it manually.
|
||||
* The source code is *.pyi file.
|
||||
* More details:
|
||||
* English Doc:
|
||||
* https://pikadoc-en.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
* Chinese Doc:
|
||||
* http://pikapython.com/doc/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html
|
||||
*/
|
||||
|
||||
#ifndef __builtins_GeneratorExit__H
|
||||
#define __builtins_GeneratorExit__H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../pikascript-core/PikaObj.h"
|
||||
|
||||
PikaObj *New_builtins_GeneratorExit(Args *args);
|
||||
|
||||
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue