forked from wd8rde/libExtio_genesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdpacket.cpp
154 lines (135 loc) · 3.31 KB
/
cmdpacket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <string.h>
#include "cmdpacket.h"
#define LOG_ERR(...) {fprintf(stderr,__VA_ARGS__);}
#define LOG_INFO(...) {fprintf(stderr,__VA_ARGS__);}
//#define DO_ANNOYING
#ifdef DO_ANNOYING
#define LOG_ANNOYING(...) {fprintf(stderr,__VA_ARGS__);fflush(stderr);}
#else
#define LOG_ANNOYING(...) {}
#endif
CmdPacket::CmdPacket()
{
memset( (m_packet), 0, GENESIS_PACKET_LEN);
}
CmdPacket::CmdPacket(tGenesisPacket packet)
{
SetPacket(packet);
}
CmdPacket::CmdPacket(tp_constGenesisCmd cmd, tconstGenesisArg1 arg1, tconstGenesisArg2 arg2)
{
memset( (m_packet), 0, GENESIS_PACKET_LEN);
SetPacket(cmd,arg1,arg2);
}
void CmdPacket::SetPacket(tGenesisPacket packet)
{
if (NULL != packet)
{
memcpy(m_packet, packet, GENESIS_PACKET_LEN);
}
}
void CmdPacket::SetPacket(tp_constGenesisCmd cmd, tconstGenesisArg1 arg1, tconstGenesisArg2 arg2)
{
SetCmd(cmd);
SetArg1(arg1);
SetArg2(arg2);
}
void CmdPacket::SetCmd(tp_constGenesisCmd cmd)
{
if (NULL == cmd)
{
return;
}
//command are padded with spaces
memset( (m_packet + GENESIS_COMMAND_OFFSET), 0x20, GENESIS_COMMAND_LENGTH);
size_t length = strnlen(cmd, GENESIS_COMMAND_LENGTH);
// do not include the terminating 0.
memcpy( (m_packet + GENESIS_COMMAND_OFFSET), cmd, length);
}
void CmdPacket::SetArg1(tconstGenesisArg1 arg)
{
if (NULL == arg)
{
return;
}
memset( (m_packet + GENESIS_ARG1_OFFSET), 0, GENESIS_ARG1_LENGTH);
memcpy( (m_packet + GENESIS_ARG1_OFFSET), arg, GENESIS_ARG1_LENGTH);
}
void CmdPacket::SetArg2(tconstGenesisArg2 arg)
{
if (NULL == arg)
{
return;
}
memset( (m_packet + GENESIS_ARG2_OFFSET), 0, GENESIS_ARG2_LENGTH);
memcpy( (m_packet + GENESIS_ARG2_OFFSET), arg, GENESIS_ARG2_LENGTH);
}
CmdPacket::tGenesisPacket CmdPacket::GetPacket()
{
return m_packet;
}
int CmdPacket::GetCmd(char cmd[])
{
if (GENESIS_COMMAND_LENGTH > sizeof cmd)
{
return -1;
}
memcpy(cmd, (m_packet + GENESIS_COMMAND_OFFSET), GENESIS_COMMAND_LENGTH);
return 0;
}
int CmdPacket::GetArg1(char arg[])
{
if (GENESIS_ARG1_LENGTH > sizeof arg)
{
return -1;
}
memcpy(arg, (m_packet + GENESIS_ARG1_OFFSET), GENESIS_ARG1_LENGTH);
return 0;
}
int CmdPacket::GetArg2(char arg[])
{
if (GENESIS_ARG2_LENGTH > sizeof arg)
{
return -1;
}
memcpy(arg, (m_packet + GENESIS_ARG2_OFFSET), GENESIS_ARG2_LENGTH);
return 0;
}
#include <ctype.h>
void CmdPacket::DumpPacket()
{
bool stop =false;
char addr[1024];
char buf[1024];
char buf2[1024];
char *a = &addr[0];
char *p = &buf[0];
char *q = &buf2[0];
for(int i=0; i< GENESIS_PACKET_LEN; i++)
{
a += sprintf(a,"%02x ", i);
p += sprintf(p,"%02x ", m_packet[i]);
q += sprintf(q,"%c ", isprint?m_packet[i]:'.');
}
LOG_ANNOYING("0x%s\n",&addr[0]);
LOG_ANNOYING("0x%s\n",&buf[0]);
LOG_ANNOYING(" %s\n",&buf2[0]);
}
bool CmdPacket::operator==(const CmdPacket &other) const
{
bool result = true;
for(int i=0; i< GENESIS_PACKET_LEN; i++)
{
if(m_packet[i] != other.m_packet[i])
{
result = false;
break;
}
}
return result;
}
bool CmdPacket::operator!=(const CmdPacket &other) const
{
return !(*this == other);
}