-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathunit_test_serial_server.cpp
More file actions
147 lines (120 loc) · 3.72 KB
/
unit_test_serial_server.cpp
File metadata and controls
147 lines (120 loc) · 3.72 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
/*
* Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
* Copyright 2016 - 2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_basic_codec.hpp"
#include "erpc_serial_transport.hpp"
#include "erpc_simple_server.hpp"
#include "Logging.hpp"
#include "c_test_unit_test_common_server.h"
#include "myAlloc.hpp"
#include "test_unit_test_common_server.hpp"
#include "unit_test.h"
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
// DEFINITIONS
////////////////////////////////////////////////////////////////////////////////
#ifndef UNIT_TEST_SERIAL_PORT
#define UNIT_TEST_SERIAL_PORT "/dev/ttyS4"
#endif
#ifndef UNIT_TEST_SERIAL_BAUD
#define UNIT_TEST_SERIAL_BAUD 115200
#endif
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
using namespace erpc;
using namespace erpcShim;
class MyMessageBufferFactory : public MessageBufferFactory
{
public:
virtual MessageBuffer create()
{
uint8_t *buf = new uint8_t[1024];
return MessageBuffer(buf, 1024);
}
virtual void dispose(MessageBuffer *buf)
{
erpc_assert(buf);
if (*buf)
{
delete[] buf->get();
}
}
};
SerialTransport g_transport(UNIT_TEST_SERIAL_PORT, UNIT_TEST_SERIAL_BAUD);
MyMessageBufferFactory g_msgFactory;
BasicCodecFactory g_basicCodecFactory;
SimpleServer g_server;
int ::MyAlloc::allocated_ = 0;
Common_service *svc_common;
int main(int argc, const char *argv[])
{
// create logger instance
StdoutLogger *m_logger = new StdoutLogger();
m_logger->setFilterLevel(Logger::log_level_t::kInfo);
Log::setLogger(m_logger);
Log::info("Starting ERPC server on port '%s' with baud %d.\n", UNIT_TEST_SERIAL_PORT, UNIT_TEST_SERIAL_BAUD);
uint8_t vtime = 0;
uint8_t vmin = 1;
while (kErpcStatus_Success != g_transport.init(vtime, vmin))
;
g_server.setMessageBufferFactory(&g_msgFactory);
g_server.setTransport(&g_transport);
g_server.setCodecFactory(&g_basicCodecFactory);
add_services(&g_server);
add_common_service(&g_server);
// run server infinitely
g_server.run();
// Thread::sleep(10000000);
free(m_logger);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Common service implementations here
////////////////////////////////////////////////////////////////////////////////
void quit()
{
remove_common_service(&g_server);
remove_services(&g_server);
exit(0);
}
int32_t getServerAllocated()
{
int result = ::MyAlloc::allocated();
::MyAlloc::allocated(0);
return result;
}
class Common_server : public Common_interface
{
public:
void quit(void) { ::quit(); }
int32_t getServerAllocated(void)
{
int32_t result;
result = ::getServerAllocated();
return result;
}
};
////////////////////////////////////////////////////////////////////////////////
// Server helper functions
////////////////////////////////////////////////////////////////////////////////
void add_common_service(SimpleServer *server)
{
svc_common = new Common_service(new Common_server());
server->addService(svc_common);
}
void remove_common_service(SimpleServer *server)
{
server->removeService(svc_common);
delete svc_common->getHandler();
delete svc_common;
}
extern "C" void erpc_add_service_to_server(void *service) {}
extern "C" void erpc_remove_service_from_server(void *service) {}
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////