Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

EventHelix.com
eventstudio model object and
message flows
visualether Wireshark pcap
to call flow
5G NR call flows
architecture
LTE call flows
architecture
company contact us
support

Object Oriented Programming in C


Embedded software development is slowly moving towards object oriented
analysis, design and programming. The introduction of object oriented
technologies in some systems has not happened due to lack of C++ support
on some platforms.

This article focuses on platforms where C++ compilers are not available. The
developers working on these platforms should still be able to use object
oriented analysis and design. When it comes to final code development they
can use C.

The following sections cover an example of C++ code and its implementation
in C.

C++ Source Code: C++ source files implementing TerminalManager


and Terminal classes.
C Source Code: TerminalManager and Terminal implemented in C for a
platform that does not support C++.

C++ Source Code


Terminal Manager and Terminal C++ header and source files are shown
below: 

Terminal Manager

TerminalManager.hpp

1 // TerminalManager header file. We will be using this class

1 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

2 // as an example for Object Oriented Programming in C.


3 #include "Terminal.hpp"
4
5 class TerminalManager
6 {
7 private:
8 enum {MAX_TERMINALS=500};
9
10 Terminal terminals[MAX_TERMINALS];
11
12 Terminal *FindTerminal(int terminalId);
13
14 public:
15 TerminalManager();
16 ~TerminalManager();
17 void HandleMessage(Msg* pMsg);
18 };

terminal-manager.hpp hosted with ❤ by GitHub view raw

TerminalManager.cpp

1 // TerminalManager source file. We will be using this class


2 // as an example for Object Oriented Programming in C.
3
4 #include <stdio.h>
5 #include "TerminalManager.hpp"
6 #include "Msg.hpp"
7
8 TerminalManager::TerminalManager()
9
10 {
11 //...
12 }
13
14 TerminalManager::~TerminalManager()
15 {
16 }
17
18 void TerminalManager::HandleMessage(Msg* pMsg)
19 {
20 int status, status1;
21
22 int terminalId = pMsg->GetTerminalId();

2 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

23
24 Terminal *pTerm = FindTerminal(terminalId);
25 Terminal *pOtherTerm = NULL;
26
27 if (pTerm != NULL)
28 {
29 switch (pMsg->GetType())
30 {
31 case CREATE_TERMINAL:
32 pTerm->Activate((const TerminalCreateMsg *)pMsg);
33 break;
34 case DELETE_TERMINAL:
35 pTerm->Deactivate((const TerminalDeleteMsg *) pMsg);
36 break;
37 case RUN_DIAGNOSTICS:
38 status = pTerm->HandleRunDiagnostics((const RunDiagnosticsMsg *) pMsg);
39 break;
40 case PERFORM_SWITCHOVER:
41 pOtherTerm = FindTerminal(pMsg->GetOtherTerminalId());
42 status = pTerm->HandleOutOfService();
43 status1 = pOtherTerm->HandleInService();
44 break;
45 }
46 }
47 delete pMsg;
48 }
49
50 Terminal *TerminalManager::FindTerminal(int terminalId)
51 {
52 if (terminalId < MAX_TERMINALS)
53 {
54 return (&terminals[terminalId]);
55 }
56 else
57 {
58 return NULL;
59 }
60 }

terminal-manager.cpp hosted with ❤ by GitHub view raw

Terminal

3 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

Terminal.hpp

1 // Terminal class header file.


2
3 // Forward declaration for messages
4 class TerminalCreateMsg;
5 class TerminalDeleteMsg;
6 class RunDiagnosticsMsg;
7 class Msg;
8
9 // Terminal class
10 class Terminal
11 {
12 enum { UNKNOWN = 0 };
13 enum {OUT_OF_SERVICE=1, INSERVICE=2};
14 //...
15 int terminalId;
16 int terminalType;
17 int terminalStatus;
18 void SendMessage(Msg *pMsg);
19
20 public:
21 void Activate(const TerminalCreateMsg *pMsg);
22 void Deactivate(const TerminalDeleteMsg *pMsg);
23 int HandleRunDiagnostics(const RunDiagnosticsMsg *pMsg);
24 int HandleOutOfService();
25 int HandleInService();
26 Terminal();
27 ~Terminal();
28 };

terminal.hpp hosted with ❤ by GitHub view raw

Terminal.cpp

1 // Terminal class source file.


2
3 #include "Terminal.hpp"
4 #include "Msg.hpp"
5 void Terminal::SendMessage(Msg *pMsg)
6 {
7 //...
8 }
9

4 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

10 Terminal::Terminal()
11 {
12 terminalId = UNKNOWN;
13 terminalType = UNKNOWN;
14 terminalStatus = UNKNOWN;
15 }
16
17 Terminal::~Terminal()
18 {
19 //...
20 }
21
22 int Terminal::HandleRunDiagnostics(const RunDiagnosticsMsg *pMsg)
23 {
24 int status = 1;
25 //...
26 return status;
27 }
28
29 int Terminal::HandleOutOfService()
30 {
31 int status = 1;
32 terminalStatus = OUT_OF_SERVICE;
33 //...
34 return status;
35 }
36
37 int Terminal::HandleInService()
38 {
39 int status = 1;
40 terminalStatus = INSERVICE;
41 //...
42 return status;
43 }
44
45 void Terminal::Activate(const TerminalCreateMsg *pMsg)
46 {
47 terminalId = pMsg->GetTerminalId();
48 terminalType = pMsg->GetTerminalType();
49 terminalStatus = pMsg->GetTerminalStatus();
50 //...
51
52 TerminalCreateAck *pAck = new TerminalCreateAck(terminalId, terminalStatus);

5 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

53 SendMessage(pAck);
54 }
55
56 void Terminal::Deactivate(const TerminalDeleteMsg *pMsg)
57 {
58 //...
59 terminalId = UNKNOWN;
60 terminalType = UNKNOWN;
61 terminalStatus = UNKNOWN;
62 //...
63 }

terminal.cpp hosted with ❤ by GitHub view raw

Msg.hpp

1 // Messages used by the Terminal and TerminalManager classes.


2
3 enum MsgType
4 {
5 CREATE_TERMINAL,
6 DELETE_TERMINAL,
7 RUN_DIAGNOSTICS,
8 PERFORM_SWITCHOVER
9 };
10
11
12 class Msg
13 {
14 //...
15 int msgType;
16 int terminalType;
17 int terminalId;
18 int otherTerminalId;
19 int terminalStatus;
20
21 public:
22 MsgType GetType() const;
23 int GetTerminalId() const;
24 int GetOtherTerminalId() const;
25 int GetTerminalType() const;
26 };
27
28 // Message used to create a terminal

6 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

29 class TerminalCreateMsg : public Msg


30 {
31 public:
32 int GetTerminalStatus() const;
33 };
34
35 // Acknowledgement to Terminal Create message.
36 class TerminalCreateAck : public Msg
37 {
38 public:
39 TerminalCreateAck(int terminalId, int terminalStatus);
40 };
41
42 // Terminal Delete message
43 class TerminalDeleteMsg : public Msg
44 {
45 };

msg.hpp hosted with ❤ by GitHub view raw

C Source Code
Terminal Manager and Terminal C header and source files are shown below.
The important points to note are:

Data members of a class map to C structures


Methods of a class map to C functions with the "data member" structure
pointer as the first parameter.

TerminalManager

TerminalManager.h

1 /*
2 TerminalManager header file. We will be using this class
3 as an example for Object Oriented Programming in C.
4 */
5
6 #include "Terminal.h"
7
8 #define MAX_TERMINALS 500
9
10 /* Structure contains all data used by the Terminal Manager. */
11 typedef struct

7 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

12 {
13 Terminal terminals[MAX_TERMINALS];
14 } TerminalManager;
15
16 /*
17 ANSI C Function prototypes of all functions that operate
18 on the TerminalManager structure.
19 */
20 void TerminalManager_Construct(TerminalManager *pMgr);
21 void TerminalManager_Destroy(TerminalManager *pMgr);
22 void TerminalManager_HandleMessage(TerminalManager *pMgr, Msg* pMsg);

terminal-manager.h hosted with ❤ by GitHub view raw

TerminalManager.c

1 /*
2 TerminalManager source file. We will be using this class
3 as an example for Object Oriented Programming in C.
4 */
5
6 #include <stdio.h>
7 #include "TerminalManager.h"
8 #include "Msg.h"
9 #include <stdlib.h>
10
11 Terminal *TerminalManager_FindTerminal(TerminalManager *pMgr, int terminalId)
12 {
13 if (terminalId < MAX_TERMINALS)
14 {
15 return (&(pMgr->terminals[terminalId]));
16 }
17 else
18 {
19 return NULL;
20 }
21 }
22
23 void TerminalManager_Construct(TerminalManager *pMgr)
24 {
25 int i;
26 /*
27 C will not call construction functions, so loop through all call the
28 construction functions for all terminals.

8 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

29 */
30 for (i=0; i < MAX_TERMINALS; i++)
31 {
32 Terminal_Construct(&(pMgr->terminals[i]));
33 }
34 }
35
36 void TerminalManager_Destroy(TerminalManager *pMgr)
37 {
38 int i;
39 /*
40 C will not call destruction functions, so loop through all call the
41 destruction functions for all terminals.
42 */
43 for (i=0; i < MAX_TERMINALS; i++)
44 {
45 Terminal_Destroy(&(pMgr->terminals[i]));
46 }
47 }
48
49 void TerminalManager_HandleMessage(TerminalManager *pMgr, Msg* pMsg)
50 {
51 int status, status1;
52
53 int terminalId = pMsg->terminalId;
54
55 Terminal *pTerm = TerminalManager_FindTerminal(pMgr, terminalId);
56 Terminal *pOtherTerm = NULL;
57
58 /*
59 Switch on the message type and invoke the Terminal's message handlers for
60 the terminal specified in the message. Here the terminal manager takes
61 care of indexing into the terminal structure and it passes the pointer
62 to the terminal handler functions. Due to this design, implementation
63 of the terminal handler functions just focus on handling the specified
64 terminal.
65 */
66 if (pTerm != NULL)
67 {
68 switch (pMsg->msgType)
69 {
70 case CREATE_TERMINAL:
71 Terminal_Activate(pTerm, (const TerminalCreateMsg *)pMsg);

9 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

72 break;
73 case DELETE_TERMINAL:
74 Terminal_Deactivate(pTerm, (const TerminalDeleteMsg *) pMsg);
75 break;
76 case RUN_DIAGNOSTICS:
77 status = Terminal_HandleRunDiagnostics(pTerm, (const RunDiagnosticsMsg *) pMsg);
78 break;
79 case PERFORM_SWITCHOVER:
80 pOtherTerm = TerminalManager_FindTerminal(pMgr, pMsg->otherTerminalId);
81 status = Terminal_HandleOutOfService(pTerm);
82 status1 = Terminal_HandleInService(pOtherTerm);
83 break;
84 }
85 }
86 free(pMsg);
87 }

terminal-manager.c hosted with ❤ by GitHub view raw

Terminal

Terminal.h

1 /* Terminal struct header file. */


2
3 #include "Msg.h"
4
5 #define UNKNOWN 0
6 #define OUT_OF_SERVICE 1
7 #define INSERVICE 2
8
9 /* Terminal struct */
10 typedef struct
11 {
12 /*...*/
13 int terminalId;
14 int terminalType;
15 int terminalStatus;
16 } Terminal;
17
18 /*
19 Prototypes for Terminal structure related functions. Helper
20 functions needed by these functions are marked static are not
21 included here.

10 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

22 */
23
24 void Terminal_Activate(Terminal *pTerm, const TerminalCreateMsg *pMsg);
25 void Terminal_Deactivate(Terminal *pTerm, const TerminalDeleteMsg *pMsg);
26 int Terminal_HandleRunDiagnostics(Terminal *pTerm, const RunDiagnosticsMsg *pMsg);
27 int Terminal_HandleOutOfService(Terminal *pTerm);
28 int Terminal_HandleInService(Terminal *pTerm);
29 void Terminal_Construct(Terminal *pTerm);
30 void Terminal_Destroy(Terminal *pTerm);

terminal.h hosted with ❤ by GitHub view raw

Terminal.c

1 /* Terminal struct source file. */


2
3 #include "Terminal.h"
4 #include "Msg.h"
5 #include <stdlib.h>
6
7 /*
8 Terminal_SendMessage is not visible to outside the Terminal.c file.
9 This is equivalent to a private method in C++.
10 */
11
12 static void Terminal_SendMessage(Terminal *pTerm, Msg *pMsg)
13 {
14 /*...*/
15 }
16
17 void Terminal_Construct(Terminal *pTerm)
18 {
19 pTerm->terminalId = UNKNOWN;
20 pTerm->terminalType = UNKNOWN;
21 pTerm->terminalStatus = UNKNOWN;
22 }
23
24 void Terminal_Destroy(Terminal *pTerm)
25 {
26 /*...*/
27 }
28
29 int Terminal_HandleRunDiagnostics(Terminal *pTerm, const RunDiagnosticsMsg *pMsg)
30 {

11 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

31 int status = 1;
32 /*...*/
33 return status;
34 }
35
36 int Terminal_HandleOutOfService(Terminal *pTerm)
37 {
38 int status = 1;
39 pTerm->terminalStatus = OUT_OF_SERVICE;
40 /*...*/
41 return status;
42 }
43
44 int Terminal_HandleInService(Terminal *pTerm)
45 {
46 int status = 1;
47 pTerm->terminalStatus = INSERVICE;
48 /*...*/
49 return status;
50 }
51
52 void Terminal_Activate(Terminal *pTerm, const TerminalCreateMsg *pMsg)
53 {
54 TerminalCreateAck *pAck;
55 pTerm->terminalId = pMsg->header.terminalId;
56 pTerm->terminalType = pMsg->header.terminalType;
57 pTerm->terminalStatus = pMsg->header.terminalStatus;
58 /*...*/
59
60 pAck = (TerminalCreateAck *)malloc(sizeof(TerminalCreateAck));
61 pAck->header.terminalId = pTerm->terminalId;
62 pAck->header.terminalStatus = pTerm->terminalStatus;
63
64 Terminal_SendMessage(pTerm, (Msg *)pAck);
65 }
66
67 void Terminal_Deactivate(Terminal *pTerm, const TerminalDeleteMsg *pMsg)
68 {
69 /*...*/
70 pTerm->terminalId = UNKNOWN;
71 pTerm->terminalType = UNKNOWN;
72 pTerm->terminalStatus = UNKNOWN;
73 /*...*/

12 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

74 }

terminal.c hosted with ❤ by GitHub view raw

Msg.h

1 /* Messages used by the Terminal and TerminalManager classes. */


2
3 #ifndef MSG_H
4 #define MSG_H
5
6 enum MsgType
7 {
8 CREATE_TERMINAL,
9 DELETE_TERMINAL,
10 RUN_DIAGNOSTICS,
11 PERFORM_SWITCHOVER
12 };
13
14 typedef struct
15 {
16 /*...*/
17 int msgType;
18 int terminalType;
19 int terminalId;
20 int otherTerminalId;
21 int terminalStatus;
22 } Msg;
23
24 /* Message used to create a terminal. */
25 typedef struct
26 {
27 Msg header;
28 }TerminalCreateMsg;
29
30 /* Acknowledgement to Terminal Create message. */
31 typedef struct
32 {
33 Msg header;
34 } TerminalCreateAck;
35
36 /* Terminal Delete message */
37 typedef struct
38 {

13 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

39 Msg header;
40 } TerminalDeleteMsg;
41
42
43 typedef struct
44 {
45 Msg header;
46 } RunDiagnosticsMsg;
47 #endif

msg.h hosted with ❤ by GitHub view raw

Explore More
Compare C++ and the equivalent C code for class declarations and
method invocation
Compare C++ and the equivalent C code for inheritance and virtual
functions.

EventStudio

call flow gallery


sequence diagrams
use cases & more
testimonials
download free trial

VisualEther

Wireshark gallery
visualize Wireshark
auto diagnose
select fields
download free trial

Telecom+networking

5G NR tutorials and call flows


5G NR blog
LTE tutorials and call flows
Long Term Evolution blog
TCP/IP flows | blog

Software Design

14 of 15 16/02/2020 16:27
Object Oriented Programming in C http://www.eventhelix.com/realtimemantra/basi...

object oriented design


design patterns
embedded design
fault handling
Software Design blog

Follow

medium
twitter
linkedin
facebook

Company

contact us
blog

© 2019  EventHelix.com Inc.

15 of 15 16/02/2020 16:27

You might also like