项目保密。在此记录一些知识点:
Linux c 读取文件可以一行一行的读:read = getline(&line, &len, fp), windows的.txt文件的每一行结尾符是"\r\n", 而linux是"\n"
Linux c 向文件中写入空行:fwrite("\r\n", 1, 2, fp)
一个调试技巧:#ifdef DEBUG #else #endif
字符串复制函数: memcpy() 比strcpy()更好用,出错更少
结构体或者数组初始化用它更方便: memset()
switch()函数只能匹配数字,不能匹配字符串。字符串匹配用strcmp()更好
u8,u16,u32 : #define u16 unsigned short #define u8 unsigned char
悄悄的把源码贴出来:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/stat.h>
#include <sys/time.h>
#define u16 unsigned short
#define u8 unsigned char
#define SIPCMSG_PARAM_SIZE 150
#define MAX 150
#define DEBUG
// Main CMD
#define SIPC_MAIN_CMD_PWR 0x01
#define SIPC_MAIN_CMD_DISP 0x07
#define SIPC_MAIN_CMD_NET 0x08
#define SIPC_MAIN_CMD_GPRS 0x0D
#define SIPC_MAIN_CMD_GEN 0x80
// Sub CMD for Network
#define SIPC_NET_PREFERRED_PLMN 0x01
#define SIPC_NET_PLMN_SELECTION 0x02
#define SIPC_NET_SERVING_NETWORK 0x03
#define SIPC_NET_PLMN_LIST 0x04
#define SIPC_NET_NETWORK_REGISTRATION 0x05
#define SIPC_NET_SUBSCRIBER_NUM 0x06
#define SIPC_NET_BAND_SELECTION 0x07
#define SIPC_NET_SERVICE_DOMAIN_CFG 0x08
#define SIPC_NET_POWER_ON_ATTACH_CFG 0x09
#define SIPC_NET_MODE_SELECT 0x0A
#define SIPC_NET_ACQUISITION_ORDER 0x0B
#define SIPC_NET_NETWORK_IDENTITY 0x0C
#define SIPC_NET_HANDOFF_DATA_CONNECTION 0x0F
#define SIPC_NET_CELL_INFORMATION 0x11
#define SIPC_NET_RAT_INFO_EMERGENCY 0x12
#define SIPC_NET_CSG_SEARCH 0x13
#define SIPC_NET_DUAL_STANBY_PREFERENCE 0x16
#define SIPC_NET_DOMAIN_SPECIFIC_RESTRICT 0x17
#define SIPC_NET_ACB_INFORMATION 0x18
#define SIPC_NET_SSAC_INFORMATION 0x19
#define SIPC_NET_VOWIFI_HO_THRESHOLD 0x1C
#define SIPC_NET_LTE_BAND_PRIORITY 0x20
#define SIPC_NET_LTE_ROAMING 0x21
#define SIPC_NET_LTE_CA 0x22
#define SIPC_NET_VARIOUS_NAS_TIMERS 0x26
// Sub CMD for Power
#define SIPC_PWR_PHONE_POWER_UP 0x01
#define SIPC_PWR_PHONE_POWER_OFF 0x02
#define SIPC_PWR_PHONE_RESET 0x03
#define SIPC_PWR_BATTERY_STATUS 0x04
#define SIPC_PWR_PHONE_UNDEFINED_5 0x05
#define SIPC_PWR_PHONE_UNDEFINED_6 0x06
#define SIPC_PWR_PHONE_STATE 0x07
// Sub CMD for Display
#define SIPC_DISP_ICON_INFO 0x01
#define SIPC_DISP_RSSI_INFO 0x06
// Sub CMD for GPRS
#define SIPC_GPRS_DEFINE_PDP_CONTEXT 0x01
#define SIPC_GPRS_QOS_PROFILE 0x02
#define SIPC_GPRS_PS_ATTACH_DETACH 0x03
#define SIPC_GPRS_PDP_CONTEXT_ACT_DEACT 0x04
#define SIPC_GPRS_ENTER_DATA_STATE 0x05
#define SIPC_GPRS_SHOW_PDP_ADDRESS 0x06
#define SIPC_GPRS_MOBILE_STATION_CLASS 0x07
#define SIPC_GPRS_3G_QOS_PROFILE 0x08
#define SIPC_GPRS_MULTIPLE_PDP_IP 0x09
#define SIPC_GPRS_DEFINE_SECONDARY_PDP_CONTEXT 0x0A
#define SIPC_GPRS_TRAFFIC_FLOW_TEMPLATE 0x0B
#define SIPC_GPRS_HSDPA_STATUS 0x0C
#define SIPC_GPRS_CURRENT_SESSION_DATA_COUNTER 0x0D
#define SIPC_GPRS_FORCE_DATA_DORMANT 0x0E
#define SIPC_GPRS_PIN_CONTROL 0x0F
#define SIPC_GPRS_GPRS_CALL_STATUS 0x10
#define SIPC_GPRS_GPRS_PORT_LIST 0x11
#define SIPC_GPRS_LTE_QOS_PROFILE 0x12
#define SIPC_GPRS_LTE_ATTACH_APN_INFO 0x14
#define SIPC_GPRS_EPDG_HANDOVER 0x15
#define SIPC_GPRS_EPDG_STATUS 0x16
#define SIPC_GPRS_FD_INFORMATION 0x17
#define SIPC_GPRS_LTE_CA_STATUS 0x19
#define SIPC_GPRS_GPRS_BACK_OFF_TIMER_T3396 0x1A
#define SIPC_GPRS_SET_APN_INFO 0x1B
#define SIPC_GPRS_TRAFFIC_CHANNEL_STATUS 0x1C
#define SIPC_GPRS_IMS_TEST_MODE_STATUS 0x21
// Sub CMD for General Resp
#define SIPC_GEN_RSP 0x01
// MSG TYPE
#define EXE 0x01
#define GET 0x02
#define SET 0x03
#define CNF 0x04
#define EVT 0x05
#define IND 0x01
#define RSP 0x02
#define NTF 0x03
typedef struct sipcMessage_t
{
u16 length;
u8 msgSeq;
u8 ackSeq;
u8 mainCmd;
u8 subCmd;
u8 cmdType;
u8 parameter[SIPCMSG_PARAM_SIZE];
}sipcMessage;
// TX MSG and RX MSG
u8 tx_msg_count = 0x01;
u8 rx_msg_count = 0x01;
int stop = 0; //alarm flag
//split with the space
int split(u8 line[], u8 temp[MAX][MAX])
{
int i = 0 ;
int j = 0;
int k = 0;
for(i = 0; i < strlen(line); i++)
{
if(line[i] == '\r' || line[i] == '\n' || line[i] == '\0')
{
break;
}
if(line[i] != ' ')
{
temp[j][k] = line[i];
k++;
}
if(line[i] == ' ' && line[i+1] != ' ')
{
j++;
k = 0;
}
}
return j+1;
}
void padding(u8 line[], sipcMessage *sipc)
{
u8 temp[MAX][MAX] = {0};
u8 mainCmd[MAX] = "";
u8 subCmd[MAX] = "";
u8 cmdType[MAX] = "";
split(line, temp);
if(line[0] == 'T' && line[1] == 'X')
{
stpcpy(mainCmd, temp[1]);
stpcpy(subCmd, temp[2]);
stpcpy(cmdType, temp[3]);
}
if(line[0] == 'R' && line[1] == 'X')
{
stpcpy(mainCmd, temp[2]);
stpcpy(subCmd, temp[3]);
stpcpy(cmdType, temp[4]);
}
//main CMD
if(strcmp(mainCmd, "SIPC_MAIN_CMD_PWR") == 0)
{
sipc->mainCmd = SIPC_MAIN_CMD_PWR;
}
else if(strcmp(mainCmd, "SIPC_MAIN_CMD_DISP") == 0)
{
sipc->mainCmd = SIPC_MAIN_CMD_DISP;
}
else if(strcmp(mainCmd, "SIPC_MAIN_CMD_NET") == 0)
{
sipc->mainCmd = SIPC_MAIN_CMD_NET;
}
else if(strcmp(mainCmd, "SIPC_MAIN_CMD_GPRS") == 0)
{
sipc->mainCmd = SIPC_MAIN_CMD_GPRS;
}
else if(strcmp(mainCmd, "SIPC_MAIN_CMD_GEN") == 0)
{
sipc->mainCmd = SIPC_MAIN_CMD_GEN;
}
else
{
sipc->mainCmd = ' ';
}
//sub CMD
if(strcmp(subCmd, "SIPC_NET_PREFERRED_PLMN") == 0)
{
sipc->subCmd = SIPC_NET_PREFERRED_PLMN;
}
else if(strcmp(subCmd, "SIPC_NET_PLMN_SELECTION") == 0)
{
sipc->subCmd = SIPC_NET_PLMN_SELECTION;
}
else if(strcmp(subCmd, "SIPC_NET_SERVING_NETWORK") == 0)
{
sipc->subCmd = SIPC_NET_SERVING_NETWORK;
}
else if(strcmp(subCmd, "SIPC_NET_PLMN_LIST") == 0)
{
sipc->subCmd = SIPC_NET_PLMN_LIST;
}
else if(strcmp(subCmd, "SIPC_NET_NETWORK_REGISTRATION") == 0)
{
sipc->subCmd = SIPC_NET_NETWORK_REGISTRATION;
}
else if(strcmp(subCmd, "SIPC_NET_SUBSCRIBER_NUM") == 0)
{
sipc->subCmd = SIPC_NET_SUBSCRIBER_NUM;
}
else if(strcmp(subCmd, "SIPC_NET_BAND_SELECTION") == 0)
{
sipc->subCmd = SIPC_NET_BAND_SELECTION;
}
else if(strcmp(subCmd, "SIPC_NET_SERVICE_DOMAIN_CFG ") == 0)
{
sipc->subCmd = SIPC_NET_SERVICE_DOMAIN_CFG;
}
else if(strcmp(subCmd, "SIPC_NET_POWER_ON_ATTACH_CFG") == 0)
{
sipc->subCmd = SIPC_NET_POWER_ON_ATTACH_CFG;
}
else if(strcmp(subCmd, "SIPC_NET_MODE_SELECT") == 0)
{
sipc->subCmd = SIPC_NET_MODE_SELECT;
}
else if(strcmp(subCmd, "SIPC_NET_ACQUISITION_ORDER ") == 0)
{
sipc->subCmd = SIPC_NET_ACQUISITION_ORDER ;
}
else if(strcmp(subCmd, "SIPC_NET_NETWORK_IDENTITY ") == 0)
{
sipc->subCmd = SIPC_NET_NETWORK_IDENTITY ;
}
else if(strcmp(subCmd, "SIPC_NET_HANDOFF_DATA_CONNECTION") == 0)
{
sipc->subCmd = SIPC_NET_HANDOFF_DATA_CONNECTION;
}
else if(strcmp(subCmd, "SIPC_NET_CELL_INFORMATION") == 0)
{
sipc->subCmd = SIPC_NET_CELL_INFORMATION;
}
else if(strcmp(subCmd, "SIPC_NET_RAT_INFO_EMERGENCY") == 0)
{
sipc->subCmd = SIPC_NET_RAT_INFO_EMERGENCY;
}
else if(strcmp(subCmd, "SIPC_NET_CSG_SEARCH") == 0)
{
sipc->subCmd = SIPC_NET_CSG_SEARCH;
}
else if(strcmp(subCmd, "SIPC_NET_DUAL_STANBY_PREFERENCE") == 0)
{
sipc->subCmd = SIPC_NET_DUAL_STANBY_PREFERENCE;
}
else if(strcmp(subCmd, "SIPC_NET_DOMAIN_SPECIFIC_RESTRICT") == 0)
{
sipc->subCmd = SIPC_NET_DOMAIN_SPECIFIC_RESTRICT;
}
else if(strcmp(subCmd, "SIPC_NET_ACB_INFORMATION") == 0)
{
sipc->subCmd = SIPC_NET_ACB_INFORMATION;
}
else if(strcmp(subCmd, "SIPC_NET_SSAC_INFORMATION") == 0)
{
sipc->subCmd = SIPC_NET_SSAC_INFORMATION;
}
else if(strcmp(subCmd, "SIPC_NET_VOWIFI_HO_THRESHOLD") == 0)
{
sipc->subCmd = SIPC_NET_VOWIFI_HO_THRESHOLD;
}
else if(strcmp(subCmd, "SIPC_NET_LTE_BAND_PRIORITY") == 0)
{
sipc->subCmd = SIPC_NET_LTE_BAND_PRIORITY;
}
else if(strcmp(subCmd, "SIPC_NET_LTE_ROAMING") == 0)
{
sipc->subCmd = SIPC_NET_LTE_ROAMING;
}
else if(strcmp(subCmd, "SIPC_NET_LTE_CA") == 0)
{
sipc->subCmd = SIPC_NET_LTE_CA;
}
else if(strcmp(subCmd, "SIPC_NET_VARIOUS_NAS_TIMERS") == 0)
{
sipc->subCmd = SIPC_NET_VARIOUS_NAS_TIMERS;
}
else if(strcmp(subCmd, "SIPC_PWR_PHONE_POWER_UP") == 0)
{
sipc->subCmd = SIPC_PWR_PHONE_POWER_UP;
}
else if(strcmp(subCmd, "SIPC_PWR_PHONE_POWER_OFF") == 0)
{
sipc->subCmd = SIPC_PWR_PHONE_POWER_OFF;
}
else if(strcmp(subCmd, "SIPC_PWR_PHONE_RESET") == 0)
{
sipc->subCmd = SIPC_PWR_PHONE_RESET;
}
else if(strcmp(subCmd, "SIPC_PWR_BATTERY_STATUS") == 0)
{
sipc->subCmd = SIPC_PWR_BATTERY_STATUS;
}
else if(strcmp(subCmd, "SIPC_PWR_PHONE_UNDEFINED_5 ") == 0)
{
sipc->subCmd = SIPC_PWR_PHONE_UNDEFINED_5 ;
}
else if(strcmp(subCmd, "SIPC_PWR_PHONE_UNDEFINED_6") == 0)
{
sipc->subCmd = SIPC_PWR_PHONE_UNDEFINED_6;
}
else if(strcmp(subCmd, "SIPC_PWR_PHONE_STATE") == 0)
{
sipc->subCmd = SIPC_PWR_PHONE_STATE;
}
else if(strcmp(subCmd, "SIPC_DISP_ICON_INFO") == 0)
{
sipc->subCmd = SIPC_DISP_ICON_INFO;
}
else if(strcmp(subCmd, "SIPC_DISP_RSSI_INFO") == 0)
{
sipc->subCmd = SIPC_DISP_RSSI_INFO ;
}
else if(strcmp(subCmd, "SIPC_GPRS_DEFINE_PDP_CONTEXT") == 0)
{
sipc->subCmd = SIPC_GPRS_DEFINE_PDP_CONTEXT;
}
else if(strcmp(subCmd, "SIPC_GPRS_QOS_PROFILE") == 0)
{
sipc->subCmd = SIPC_GPRS_QOS_PROFILE;
}
else if(strcmp(subCmd, "SIPC_GPRS_PS_ATTACH_DETACH") == 0)
{
sipc->subCmd = SIPC_GPRS_PS_ATTACH_DETACH;
}
else if(strcmp(subCmd, "SIPC_GPRS_PDP_CONTEXT_ACT_DEACT") == 0)
{
sipc->subCmd = SIPC_GPRS_PDP_CONTEXT_ACT_DEACT;
}
else if(strcmp(subCmd, "SIPC_GPRS_ENTER_DATA_STATE") == 0)
{
sipc->subCmd = SIPC_GPRS_ENTER_DATA_STATE;
}
else if(strcmp(subCmd, "SIPC_GPRS_SHOW_PDP_ADDRESS") == 0)
{
sipc->subCmd = SIPC_GPRS_SHOW_PDP_ADDRESS;
}
else if(strcmp(subCmd, "SIPC_GPRS_MOBILE_STATION_CLASS") == 0)
{
sipc->subCmd = SIPC_GPRS_MOBILE_STATION_CLASS;
}
else if(strcmp(subCmd, "SIPC_GPRS_3G_QOS_PROFILE") == 0)
{
sipc->subCmd = SIPC_GPRS_3G_QOS_PROFILE;
}
else if(strcmp(subCmd, "SIPC_GPRS_MULTIPLE_PDP_IP") == 0)
{
sipc->subCmd = SIPC_GPRS_MULTIPLE_PDP_IP;
}
else if(strcmp(subCmd, "SIPC_GPRS_DEFINE_SECONDARY_PDP_CONTEXT") == 0)
{
sipc->subCmd = SIPC_GPRS_DEFINE_SECONDARY_PDP_CONTEXT;
}
else if(strcmp(subCmd, "SIPC_GPRS_TRAFFIC_FLOW_TEMPLATE") == 0)
{
sipc->subCmd = SIPC_GPRS_TRAFFIC_FLOW_TEMPLATE;
}
else if(strcmp(subCmd, "SIPC_GPRS_HSDPA_STATUS") == 0)
{
sipc->subCmd = SIPC_GPRS_HSDPA_STATUS;
}
else if(strcmp(subCmd, "SIPC_GPRS_CURRENT_SESSION_DATA_COUNTER") == 0)
{
sipc->subCmd = SIPC_GPRS_CURRENT_SESSION_DATA_COUNTER;
}
else if(strcmp(subCmd, "SIPC_GPRS_FORCE_DATA_DORMANT") == 0)
{
sipc->subCmd = SIPC_GPRS_FORCE_DATA_DORMANT;
}
else if(strcmp(subCmd, "SIPC_GPRS_PIN_CONTROL") == 0)
{
sipc->subCmd = SIPC_GPRS_PIN_CONTROL;
}
else if(strcmp(subCmd, "SIPC_GPRS_GPRS_CALL_STATUS") == 0)
{
sipc->subCmd = SIPC_GPRS_GPRS_CALL_STATUS;
}
else if(strcmp(subCmd, "SIPC_GPRS_GPRS_PORT_LIST") == 0)
{
sipc->subCmd = SIPC_GPRS_GPRS_PORT_LIST;
}
else if(strcmp(subCmd, "SIPC_GPRS_LTE_QOS_PROFILE") == 0)
{
sipc->subCmd = SIPC_GPRS_LTE_QOS_PROFILE;
}
else if(strcmp(subCmd, "SIPC_GPRS_LTE_ATTACH_APN_INFO") == 0)
{
sipc->subCmd = SIPC_GPRS_LTE_ATTACH_APN_INFO;
}
else if(strcmp(subCmd, "SIPC_GPRS_EPDG_HANDOVER") == 0)
{
sipc->subCmd = SIPC_GPRS_EPDG_HANDOVER;
}
else if(strcmp(subCmd, "SIPC_GPRS_EPDG_STATUS") == 0)
{
sipc->subCmd = SIPC_GPRS_EPDG_STATUS;
}
else if(strcmp(subCmd, "SIPC_GPRS_FD_INFORMATION") == 0)
{
sipc->subCmd = SIPC_GPRS_FD_INFORMATION;
}
else if(strcmp(subCmd, "SIPC_GPRS_LTE_CA_STATUS") == 0)
{
sipc->subCmd = SIPC_GPRS_LTE_CA_STATUS;
}
else if(strcmp(subCmd, "SIPC_GPRS_GPRS_BACK_OFF_TIMER_T3396") == 0)
{
sipc->subCmd = SIPC_GPRS_GPRS_BACK_OFF_TIMER_T3396;
}
else if(strcmp(subCmd, "SIPC_GPRS_SET_APN_INFO") == 0)
{
sipc->subCmd = SIPC_GPRS_SET_APN_INFO;
}
else if(strcmp(subCmd, "SIPC_GPRS_TRAFFIC_CHANNEL_STATUS") == 0)
{
sipc->subCmd = SIPC_GPRS_TRAFFIC_CHANNEL_STATUS;
}
else if(strcmp(subCmd, "SIPC_GPRS_IMS_TEST_MODE_STATUS") == 0)
{
sipc->subCmd = SIPC_GPRS_IMS_TEST_MODE_STATUS;
}
else if(strcmp(subCmd, "SIPC_GEN_RSP") == 0)
{
sipc->subCmd = SIPC_GEN_RSP;
}
else
{
sipc->subCmd = ' ';
}
//CMD Type
if(strcmp(cmdType, "EXE") == 0)
{
sipc->cmdType = EXE;
}
else if(strcmp(cmdType, "GET") == 0)
{
sipc->cmdType = GET;
}
else if(strcmp(cmdType, "SET") == 0)
{
sipc->cmdType = SET;
}
else if(strcmp(cmdType, "CNF") == 0)
{
sipc->cmdType = CNF;
}
else if(strcmp(cmdType, "EVT") == 0)
{
sipc->cmdType = EVT;
}
else if(strcmp(cmdType, "IND") == 0)
{
sipc->cmdType = IND;
}
else if(strcmp(cmdType, "RSP") == 0)
{
sipc->cmdType = RSP;
}
else if(strcmp(cmdType, "NTF") == 0)
{
sipc->cmdType = NTF;
}
else
{
sipc->cmdType = ' ';
}
}
void alarmhandle(int sig)
{
stop = 1;
}
void set_alarm(unsigned int time)
{
int ret ;
struct itimerval tick;
tick.it_value.tv_sec = time;
tick.it_value.tv_usec = 0;
tick.it_interval.tv_sec = 0;
tick.it_interval.tv_usec = 0;
signal(SIGALRM, alarmhandle);
ret = setitimer(ITIMER_REAL, &tick, NULL);
if(ret != 0){
printf("Set timer error.\n");
return ;
}
printf("Wait!\n");
}
int Highlight(u8 data[MAX][MAX], sipcMessage *sipc_recv)
{
int i, j;
sipcMessage sipc;
u8 temp[MAX][MAX] = {0};
for(i = 0; i < MAX; i++)
{
split(data[i], temp);
if(strcmp(temp[0], "HIGHLIGHT") == 0)
{
for(j = i; j < MAX; j++)
{
padding(data[j], &sipc);
if(sipc.mainCmd == sipc_recv->mainCmd && sipc.subCmd == sipc_recv->subCmd)
{
return 1;
}
if(strcmp(data[j], "") == 0)
{
return 0;
}
}
}
}
return 0;
}
void main(void)
{
sipcMessage sipc; //from senario.txt
sipcMessage sipc_recv; //from driver
FILE * fp; //.txt file
int fd; //driver
u8 data[MAX][MAX] = {0};
u8 temp[MAX][MAX] = {0};
u8 previous[MAX][MAX] = {0};
u8 *line = NULL;
size_t len = 0;
ssize_t read;
int i = 0;
int j = 0;
int m = 0;
int n = 0;
int number; //senario file total line
unsigned int sec;
int pre = -1; //previous CMD
int ret;
memset(&sipc, 0, sizeof(sipcMessage));
//read senario.txt
fp = fopen("./senario.txt", "r");
if(fp == NULL)
{
exit(EXIT_FAILURE);
}
while((read = getline(&line, &len, fp)) != -1)
{
if(read == 2)
{
stpcpy(data[i], "");
}
else if(line[0] == '/' && line[1] == '/')
{
continue;
}
else
{
stpcpy(data[i], line);
}
i++;
}
number = i ;
printf("number = %d \n", number);
for(i = 0 ; i < number; i++)
{
if(strcmp(data[i], "") == 0)
continue;
printf("data[%d] = %s", i, data[i]);
//printf("data[%d][0] = %c \t data[%d][1] = %c \n", i, data[i][0], i, data[i][1]);
}
printf("\n");
if(line)
free(line);
fclose(fp);
//write result.txt and padding sipcMessage
fp = fopen("./result.txt", "w+");
if(fp == NULL)
{
exit(EXIT_FAILURE);
}
fd = open("/dev/my_misc_dev", O_RDWR);
if(fd != -1)
{
exit(EXIT_FAILURE);
}
i = 0;
while(i < number)
{
//Send CMD
if(data[i][0] == 'T' && data[i][1] == 'X'){
//put into result file
fwrite(data[i], strlen(data[i]), 1, fp);
n = 0;
for(j = i+1; j < number; j++)
{
if(strcmp(data[j], "") == 0)
{
break;
}
else
{
fwrite(data[j], strlen(data[j]), 1, fp);
if(n > 0)
{
sipc.parameter[n] = ' ';
n++;
}
// padding sipcMessage parameter
for(m = 0; m < strlen(data[j]); m++, n++)
{
if(data[j][m] == '\n' ||data[j][m] == '\r')
{
break;
}
else
{
sipc.parameter[n] = data[j][m];
}
}
}
}
//padding sipcMessage
sipc.msgSeq = tx_msg_count;
sipc.ackSeq = 0xFF; //default 0xFF
tx_msg_count ++;
padding(data[i], &sipc);
pre = i;
i = j;
memset(&sipc, 0, sizeof(sipcMessage));
//send to driver
#ifndef DEBUG
write(fd, (void *)&sipc, sizeof(sipc));
#endif
fwrite("\r\n", 1, 2, fp);
continue;
}
// Receive CMD
if(data[i][0] == 'R' && data[i][1] == 'X')
{
//padding sipcMessage parameter
n = 0;
for(j = i+1; j < number; j++)
{
if(strcmp(data[j], "") == 0)
{
break;
}
else
{
if(n > 0)
{
sipc.parameter[n] = ' ';
n++;
}
for(m = 0; m < strlen(data[j]); m++, n++)
{
if(data[j][m] == '\n' ||data[j][m] == '\r')
{
break;
}
else
{
sipc.parameter[n] = data[j][m];
}
}
}
}
//padding sipcMessage
sipc.msgSeq = rx_msg_count;
rx_msg_count ++;
padding(data[i], &sipc);
//set alarm
split(data[i], temp);
sec = atol(temp[1]);
printf("sec = %d \n", sec);
set_alarm(sec);
#ifndef DEBUG
while(1)
{
if (stop == 1)
break;
ret = read(fd, (void *)&sipc_recv, sizeof(sipc_recv));
}
#else
while(1)
{
if(stop == 1)
{
break;
}
}
//构造假的sipc_recv, 未完待续??
//如果上一条是TX命令,则构造应该接收的RX命令;如果上一条是RX命令,则根据本条命令构造RX 本程序只给出根据GET命令构造的RX的response命令
//get-response set-general response execute-general response
split(data[pre], previous);
if(strcmp(previous[0], "TX") == 0)
{
if(strcmp(previous[3], "GET") == 0) //根据GET命令,构造response命令
{
sipc_recv.msgSeq = rx_msg_count - 1;
sipc_recv.ackSeq = tx_msg_count - 1; //如果前一个命令是TX类型的,那么最新的TX msg sequence 就是本条RX命令的ack sequence
sipc_recv.mainCmd = SIPC_MAIN_CMD_GEN;
sipc_recv.subCmd = SIPC_GEN_RSP;
sipc_recv.cmdType = RSP;
rx_msg_count++;
}
//需要增加SET等命令回应命令 未完待续??
ret = 5;
}
//如果上一条命令是RX类型的,就根据本条RX命令,去构造sipc_recv
if(strcmp(previous[0], "RX") == 0)
{
sipc_recv.msgSeq = rx_msg_count - 1;
sipc_recv.ackSeq = 0xFF;
sipc_recv.mainCmd = sipc.mainCmd;
sipc_recv.subCmd = sipc.subCmd;
sipc_recv.cmdType = sipc.cmdType;
strcpy(sipc_recv.parameter, sipc.parameter);
rx_msg_count++;
ret = 5;
}
#endif
if(strcmp(sipc_recv.parameter, "") != 0)
{
strcat(sipc_recv.parameter, "\r\n");
}
if(ret < 1)
{
//Highlight
if(Highlight(data, &sipc_recv))
{
u8 error[MAX] = "ERROR : There is no RX MSG";
fwrite(error, strlen(error), 1, fp);
fwrite(data[i], strlen(data[i]), 1, fp);
fwrite(sipc_recv.parameter, strlen(sipc_recv.parameter), 1, fp);
}
else
{
u8 error[MAX] = "ERROR : Receive nothing";
fwrite(error, strlen(error), 1, fp);
}
}
else
{
if(sipc_recv.mainCmd == sipc.mainCmd && sipc_recv.subCmd == sipc.subCmd)
{
fwrite(data[i], strlen(data[i]), 1, fp);
fwrite(sipc_recv.parameter, strlen(sipc_recv.parameter), 1, fp);
}
else
{
u8 error[MAX] = "ERROR : Data error";
fwrite(error, strlen(error), 1, fp);
}
}
pre = i;
i = j;
ret = 0 ;
memset(&sipc, 0, sizeof(sipcMessage));
memset(&sipc_recv, 0, sizeof(sipcMessage));
fwrite("\r\n", 2, 1, fp);
continue;
}
i = i + 1 ;
}
exit(EXIT_SUCCESS);
}
readme.txt
已完成:
1.读取senario.txt文件
2.切割每条命令。根据senario.txt文件中的空格符,把整条命令切割。
3.将每条命令填充到结构体sipcMessage中
4.将处理结果写回result.txt文件
5.对于HightLight类型的命令特殊处理
6.等待接收时间的处理(设定闹钟)
待完善:
1.根据TX的命令类型构造对应的回复命令,本程序给出了TX-GET类型命令回复命令RX,但是TX其它类型的(比如SET等)待完成
2.应用程序收到RX后,如何判定此命令是正确的。此判定条件待完成。
3.用线程实现