-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxcpd.cpp
More file actions
165 lines (135 loc) · 5.17 KB
/
Copy pathxcpd.cpp
File metadata and controls
165 lines (135 loc) · 5.17 KB
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
155
156
157
158
159
160
161
162
163
164
165
/*
This routine is only a example to perform a XCP CONNECT
to an connected SLAVE on a configured (IP settings) network.
*/
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <iostream>
#include "xcpd.h"
#define SRC_PORT 5555
#define DST_PORT 5555
#define ECU_IP "127.0.0.1"
#define SEEDNKEYXCP_SO "SeedNKeyXCP.so"
int main(int argc, char *argv[])
{
int s;
struct sockaddr_in servaddr, srcaddr;
std::vector<uint8_t> data = { 0x11, 0x22, 0x33, 0x44 };
XCPMsgPtr connect_message, GetStatus, GetSeed1, SetMTA;
XCPMsgPtr Upload, ShortUpload, Download, ShortDownload, disconnect_message;
LoadSO(); // Loads the user provided dll with the Seed&Key algorithms. Sets up the GetAvailablePrivileges and ComputeKeyFromSeed function pointers.
master = new XCPMaster(TransportLayer::ETHERNET);
master->SetSeedAndKeyFunctionPointers(
GetAvailablePrivileges, // function pointer for the GetAvailablePrivileges
ComputeKeyFromSeed); // function pointer for the ComputeKeyFromSeed
// Creating socket file descriptor
if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
// Filling client information
memset(&srcaddr, 0, sizeof(srcaddr));
srcaddr.sin_family = AF_INET;
srcaddr.sin_addr.s_addr= htonl(INADDR_ANY);
srcaddr.sin_port = htons(SRC_PORT);
bind(s, (struct sockaddr *) &srcaddr, sizeof(srcaddr));
// Filling server information
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DST_PORT);
servaddr.sin_addr.s_addr = inet_addr(ECU_IP);
// perform XCP
// ################################### CONNECT ##########################################
std::cout << "Send XCP CONNECT.\n";
connect_message = master->CreateConnectMessage(ConnectPacket::ConnectMode::NORMAL);
Send(s, servaddr, std::move(connect_message));
MaxRecvsize = master->GetSlaveProperties().MaxDto;
std::cout << "\n";
// ################################# GET STATUS #########################################
std::cout << "Send XCP GET_STATUS.\n";
GetStatus = master->CreateGetStatusMessage();
Send(s, servaddr, std::move(GetStatus));
std::cout << "\n";
// unlook SLAVE with calculated key
std::cout << "Send to get seed.\n";
GetSeed1 = master->CreateGetSeedMessage(GetSeedPacket::Mode::FIRST_PART, GetSeedPacket::Resource::DAQ);
Send(s, servaddr, std::move(GetSeed1));
std::vector<XCPMsgPtr> UnlockMessages = master->CreateUnlockMessages();
Send(s, servaddr, std::move(UnlockMessages[0]));
// ################################### UPLOAD ###########################################
std::cout << "Send XCP SET_MTA + UPLOAD.\n";
SetMTA = master->CreateSetMTAMessage(0x12345678, 0);
Send(s, servaddr, std::move(SetMTA));
Upload = master->CreateUploadMessage(2);
Send(s, servaddr, std::move(Upload));
std::cout << "\n";
// ################################### DOWNLOAD #########################################
std::cout << "Send XCP SET_MTA + DOWNLOAD.\n";
SetMTA = master->CreateSetMTAMessage(0x12345678, 0);
Send(s, servaddr, std::move(SetMTA));
Download = master->CreateDownloadMessage(data);
Send(s, servaddr, std::move(Download));
std::cout << "\n";
std::cout << "Send XCP SHORT_DOWNLOAD.\n";
ShortDownload = master->CreateShortDownloadMessage(0x12345678, 0, data);
Send(s, servaddr, std::move(ShortDownload));
std::cout << "\n";
// ################################### DISCONNECT #######################################
std::cout << "Send XCP DISCONNECT.\n";
disconnect_message = master->CreateDisconnectMessage();
Send(s, servaddr, std::move(disconnect_message));
std::cout << "\n";
close(s);
return 0;
}
int LoadSO()
{
void *hGetProcIDSO;
// load the shared object
hGetProcIDSO = dlopen(SEEDNKEYXCP_SO, RTLD_LAZY);
if (!hGetProcIDSO) {
std::cout << "could not load the dynamic library" << std::endl;
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
// clear any existing error
dlerror();
// check for XCP_GetAvailablePrivileges()
GetAvailablePrivileges = (XCP_GetAvailablePrivilegesPtr_t)dlsym(hGetProcIDSO, "XCP_GetAvailablePrivileges");
if (!GetAvailablePrivileges) {
std::cout << "could not locate the function" << std::endl;
exit(EXIT_FAILURE);
}
// check for XCP_ComputeKeyFromSeed()
ComputeKeyFromSeed = (XCP_ComputeKeyFromSeedPtr_t)dlsym(hGetProcIDSO, "XCP_ComputeKeyFromSeed");
if (!GetAvailablePrivileges) {
std::cout << "could not locate the function" << std::endl;
exit(EXIT_FAILURE);
}
return 0;
}
void Send(int s, struct sockaddr_in servaddr, XCPMsgPtr message)
{
unsigned int len;
// send data
message->Serialize(bytes);
sendto(s, (const char *)bytes.data(), bytes.size(), MSG_CONFIRM,
(const struct sockaddr *) &servaddr, sizeof(servaddr));
bytes.clear();
// receive data
bytes.resize(MaxRecvsize);
master->AddSentMessage(message.get());
int recv_size = recvfrom(s, (char*)&bytes[0], MaxRecvsize, MSG_WAITALL,
(struct sockaddr *) &servaddr, &len);
bytes.resize(recv_size);
// print data
for (int i = 0; i < recv_size; i++) {
std::cout << std::hex << (int)(bytes[i] & 0xff) << " ";
}
std::cout << "\n";
XCPMsgPtr asd = master->DeserializeMessage(bytes);
}