Rhapsody - OS Features Usage Guide

Rhapsody - OS Features Usage Guide

Version : 1.0

Last Modified By : Prasad Bhat, IBM

Date : 24 Feb 2012

Table of Contents

1. Introduction

2. Multithreading

3. MessageQueues

4. Semaphores

5. Socket Programming

1. Introduction

This paper takes you through usage of Rhapsody for OS / RTOS (RealTime Operating System) specific features like Multithreading, Semaphores, Timers, MessageQueues and Sockets. The objective is to understand the feasibility of creating Platform Independent Models (PIM) through conscious usage of OXF APIs in the model.

2. Multithreading

Rhapsody applications with animation feature enabled is by default multi threaded. It is because of this nature we are able to do design level debugging (like injecting an event to a statechart). However, in some cases even design classes need to run on a separate thread. It is called as active classes in UML.

Identify the classes which should run as a seprate thread. Open a OMD in Rhapsody.

Here, the role of SPPlotReceiver is to keep listening for data and writing it to a shared location which will be read by other classes (SPDataValidator).

Make SPPlotReceiver as an active class and it runs in an infinite loop.

Add an overridden execute operation to this class with return type as OMReactive*

Add a constructor to this class and add code to start the thread.

Note: Enabling execute operation of OMThread disables the state behavior of the active class, since execute() runs infinitely.Therefore you can disable animation for this operation.

How to set thread parameters (RR, FIFO scheduling etc.,) ?

This changes the parameters in the actual API call (Ex: NTOS.cpp in oxf implementation)

Note : The thread parameters are yet to be tested on POSIX / LynxOS APIs.

3. MessageQueues

Since Rhapsody OXF supports complete implementation of MessageQueues inherently, it is suggested to use the OXF APIs in the model rather than the actual RTOS APIs. This chapter shows the same with example.

Create an oxf package and a class OMEventQueue, make both as external. This will refer to the OXF implementation.

Create an Object Model Diagram in DesignPkg.

Create a data packet to be sent over the queue.

Attach it to an event.

Call the putmessage API of OMEventQueue to send the packet over the queue.

Modify receiveSPData method

In OMEventQueue.h

Where theQueue is defined as

And putMessage is defined in os.h under class OMOSMessageQueue

//…..

In ntos.cpp (windows platform), put is realized as follows. It already has mutex lock and unlock implemented for writing to the queue.

If you want to write just a message packet (without events) use the put message in os.h

To receive message from MQ

Add a method to receive the packets.

Call the get API of OMEventQueue and cast it to the packet type.

The retreival of packet from MQ is done by the get() OXF API which is defined in OMEventQueue.h as

get() in os.h file is defined as

……under class OMOSMessageQueue

For windows platform, this operation is realized in ntos.cpp as below

Note: Mutex lock and unlock is already implemented in OXF classes.

Finally, initialize the MQ in the statechart of Builder class

And then in the constructor

Run the project –

Queue is initialized

Data is written to queue & received by the target class object and printed.

4. Semaphores

Semaphores are already implemented inherently in OXF Event Queue mechanism as seen in chapter 3. But if semaphores are explicitly required in the application code, then the following technique can be used.

Create a shared memory in the modeling project (Ex: a shared singleton class object )

See the corresponding code

GUARD_OPERATION definesan instance of class OMGuard,which will lock the declared guard.

These macros are defined in OXFGuardMacros.h file

OMProtected in turn defines the mutex operations in omprotected.cpp as below

Call the guarded operation from one of the threads.

A reader operation can be created on the other thread by adding another guarded operation in the same way.

5. Socket Programming

Rhapsody OXF supports socket programming via the OMOSSocket wrapper class.

It has 4 methods – Create, Send, Receive & Close

In the Rhapsody application model, add a constructor to a class and create a socket connection.

Note: Have modified and rebuilt the OXF code to support createOMOSSocket(), since Rhapsody supports only createOMOSConnectionPort().

Add the sending and receiving socket calls in the code (in this case state chart of Centroider class).

Run the project –

You can see that socket creation is successful (returns 1) and data is also sent (returns # of bytes).

Note: There is some issue with receiving the packets over the socket (currently working on this).

1