5

Can anyone give me an example how to draw this BLE-Stack with tikz?

BLE-Stack

EDIT

Now, I'm very close to my wished result :)

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[align=center,>=latex',font=\sffamily]
\tikzstyle{Dotted} = [draw=black,dashed,thick,rectangle,minimum width=120mm,minimum height=33mm]
\tikzstyle{Normal} = [draw=black,thick,rectangle,minimum width=108mm,minimum height=12mm]
\node[label=left:Controller,label={[label distance=5.5mm]},Dotted](Controller){};
\node[above left = 3mm and 6mm of Controller.south east,anchor=south east,Normal](PHY){LE Physical Layer\\(PHY)};
\node[above = 3mm of PHY,Normal](LL){Link Layer\\(LL)};
\node[above = 10mm of Controller.north west,anchor=north west](HCI0){};
\node[above = 10mm of Controller.north east,anchor=north east](HCI1){};
\draw[dash pattern={on 5mm off 3mm},line width=1.2mm,](HCI0) -- (HCI1) node[midway,fill=white]{Host Controller Interface\\(HCI)};
\node[above = 18mm of Controller,label=left:Host,label={[label distance=5.5mm]},Dotted,minimum height=48mm](Host){};
\node[above left = 3mm and 6mm of Host.south east,anchor=south east,Normal](L2CAP){Logical Link Control and Adaption Protocol\\(L2CAP)};
\node[above =15mm of L2CAP.north east,anchor=north east,Normal,minimum width=51mm](ATT){Attribute Protocol\\(ATT)};
\node[above = 15mm of ATT.east,anchor=east,Normal,minimum width=51mm](GATT){Generic Attribute Profile\\(GATT)};
\node[above = 15mm of L2CAP.north west,anchor=north west,Normal,minimum width=51mm](GAP){Generic Access Profile\\(GAP)};
\node[above = 15mm of GAP.north west,anchor=north west,Normal,minimum width=51mm](SMP){Security Manager Protocol\\(SMP)};
\node[above = 3mm of Host,label=left:Application,label={[label distance=5.5mm]},Normal,minimum width=120mm](Application){Application\\(APP)};
\end{tikzpicture}
\end{document}

This code results in this output

enter image description here

There are just two questions:

  1. How to start the dashed line from the left border and from the right to get a symmetrical dashed line?
  2. How to delete the insertion on the left and right of the dashed line?
14
  • 2
    what you try so far?
    – Zarko
    Commented Aug 31, 2019 at 12:51
  • 2
    Give me some time, I'm preparing something
    – Pascal
    Commented Aug 31, 2019 at 16:59
  • 4
    @Raaja please let OPs time to react! What is the advantage to close so fast? Please retract your close vote!
    – Mensch
    Commented Aug 31, 2019 at 17:18
  • 1
    @AndréC Did it!
    – Pascal
    Commented Aug 31, 2019 at 20:10
  • 2
    For the dash pattern \path[](HCI0) -- (HCI1) node[midway,fill=white](aux){Host Controller Interface\\(HCI)}; \draw [dash pattern={on 5mm off 3mm},line width=1.2mm](aux)--(HCI0)(aux)--(HCI1);
    – AndréC
    Commented Aug 31, 2019 at 20:15

4 Answers 4

6

Everything here is placed with respect to the components in the diagram. With the comments, the logic is very easy to follow.

Output

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
\tikzset{
  nopadding/.style={
    inner sep=0pt, outer sep=0pt
  },
  textBlock/.style={
    draw, text width=12em, text centered, minimum height=2em,
    nopadding, inner ysep=3pt
  },
  longTextBlock/.style={
    text width=24em, text centered, minimum height=2em,
    nopadding, inner ysep=3pt
  }
}
\begin{document}
  \begin{tikzpicture}[font=\small\sffamily]%
    \node[draw=none,minimum width=25em, nopadding] (start) at (0,0) {}; % Starting point
    % Host
    % Row 1, col 1
    \node[textBlock, below=of start.south west, anchor=west]
         (SMP){Security Manager Protocol\\(SMP)};
    % Row 1, col 2
    \node[textBlock, below=of start.south east, anchor=east]
         (GATT){Generic Attribute Profile\\(GATT)};
    % Row 2, col 1
    \node[textBlock, below of=SMP](GAP){Generic Access Profile\\(GAP)};
    % Row 2, col 2
    \node[textBlock, below of=GATT](ATT){Attribute Protocol\\(ATT)};
    % Row 3, col 1&2
    \node[fit=(SMP) (GATT), below=of GAP.west, anchor=west, draw, nopadding](L2CAP){};
    % Row 3, col 1&2 text
    \node[longTextBlock] 
         (L2CAPText)at(L2CAP.center){Logical Link Control and Adaption Protocol\\(L2CAP)};
    % Host dashed line and label
    \node[fit=(L2CAP) (SMP) (GAP) (GATT) (ATT), 
         draw, dashed, label={[black, xshift=-5pt]left:Host} ](hostFit){};
    % Application label
    \node[fit=(hostFit.west) (hostFit.east)(GAP), 
         above=of hostFit.north west, anchor=north west,
         draw, nopadding, label={[black, xshift=-5pt]left:Application}](appFit){};
    % Application text
    \node[longTextBlock] (appText) at (appFit.center){Application\\(APP)};
    % Controller dashed lines fit
    \node[fit=(hostFit.west) (hostFit.east)(GAP), 
         below=of hostFit.south west, anchor=south west,
         nopadding,draw=none](controllerDashed){};
    % Controller actual lines and text.
     \draw[dash pattern={on 7pt off 3pt}, line width=3pt]
         (controllerDashed.west) -- (controllerDashed.east)
         node[textBlock, midway,fill=white, draw=none]
         {Host Controller Interface\\(HCI)};
    % Controller
    % Row 1
    \node[fit=(L2CAP), below=of controllerDashed.south, anchor=south, 
         nopadding, draw, yshift=-5pt](controllerRow1){};
    % Row 1 text
    \node[longTextBlock] 
         (controlRow1Text) at (controllerRow1.center){LE Physical Layer\\(PHY)};
    % Row 2
    \node[fit=(controllerRow1), below of = controllerRow1, draw, 
         nopadding,draw](controllerRow2){};
    % Row 2 text
    \node[longTextBlock] 
    (controlRow2Text) at (controllerRow2.center){Link Layer\\(LL)};
    % Controller dashed lines and label
    \node[fit=(controllerRow1)(controllerRow2), draw, dashed, 
         label={[black, xshift=-5pt]left:Control}](controlFit){};
  \end{tikzpicture}
\end{document}
7

With use of TikZ libraries fit and positioning, for all positioning distances are used only values defined in node distance. This enables to write clear, concise and consistent code:

Edit: if you like to change distances of nodes' labels from nodes (on consistent way), you only need to add label distance = <desired amount> to tikzpicture options (added now to MWE below.

Also, if you like to change distance between nodes and doted nodes around it, for example have different in horizontal direction from vertical, you only need to add inner xsep and inner ysep in style for node FIT. For example:

FIT/.style = {draw, semithick, dotted, fit=#1,
              inner xsep=4mm, inner ysep=2mm},  % here you can adjust inner distance of node}, 

All aforementioned I now added to my MWE.

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{fit,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 2mm and 0mm,
   box/.style = {draw, text width=#1, inner sep=2mm, align=center},
   box/.default = 98mm,
   FIT/.style = {draw, semithick, dotted, fit=#1,
                 inner xsep=4mm, inner ysep=2mm},  % here you can adjust inner distance of node
                                                   % this adjust you need to consider at defining the width of the top nodes
label distance = 2mm,
      font = \sffamily
                        ]
\node (phy) [box]                   {LE Physical Layer\\(PHY)};
\node (lll) [box, above = of phy]   {Link Layer\\(LL)};
    \node (cntrl) [FIT=(phy) (lll), label=left:Control] {};
%
\node (hci) [box=12em,draw=none, 
             above=of cntrl]        {Host Controller Interface\\(HCI)};
\draw[line width=1.2mm, dash pattern=on 5mm off 3mm]  % solve your first question
    (lll.west |- hci) -- (hci)  
    (lll.east |- hci) -- (hci);
%
\node (cap) [box, above=of hci]     {Logical Link Control and Adaption Protocol\\(L2CAP)};
\node (gap) [box=44mm,
             above right=of cap.north west] {Generic Access Profile\\(GAP)};
\node (att) [box=44mm,
             above  left=of cap.north east] {Attribute Protocol\\(ATT)};
\node (smp) [box=44mm,
             above=of gap]          {Security Manager Protocol\\(SMP)};
\node (gap) [box=44mm,
             above=of att]          {Generic Attribute Profile\\(GATT)};
    \node (host) [FIT=(cap) (smp), label=left: Host] {};
%
\node (app) [box=106mm, above= of host,
             label=left:Application]        {Application\\(APP)};
\end{tikzpicture}
\end{document}

Result is:

enter image description here

5
  1. For the first question, simply name the node where the text is written, then draw the pattern from this node to the edges.
    \path[](HCI0) -- (HCI1) node[midway,fill=white](aux){Host Controller Interface\\(HCI)};%<--- auxiliary node (aux)
    \draw [dash pattern={on 5mm off 3mm},line width=1.2mm](aux)--(HCI0)(aux)--(HCI1);% pattern from center to edges
  1. The second question is more complex, so I suggest that you make it another question in its own right.

screenshot

\documentclass[border=5mm,tikz]{standalone}

\usetikzlibrary{arrows}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[align=center,>=latex',font=\sffamily]
\tikzstyle{Dotted} = [draw=black,dashed,thick,rectangle,minimum width=120mm,minimum height=33mm]
\tikzstyle{Normal} = [draw=black,thick,rectangle,minimum width=108mm,minimum height=12mm]
\node[label=left:Controller,label={[label distance=5.5mm]},Dotted](Controller){};
\node[above left = 3mm and 6mm of Controller.south east,anchor=south east,Normal](PHY){LE Physical Layer\\(PHY)};
\node[above = 3mm of PHY,Normal](LL){Link Layer\\(LL)};
\node[above = 10mm of Controller.north west,anchor=north west](HCI0){};
\node[above = 10mm of Controller.north east,anchor=north east](HCI1){};

% new node (aux)
\path[](HCI0) -- (HCI1) node[midway,fill=white](aux){Host Controller Interface\\(HCI)};%<--- auxiliary node (aux)
\draw [dash pattern={on 5mm off 3mm},line width=1.2mm](aux)--(HCI0)(aux)--(HCI1);% pattern from center to edges

\node[above = 18mm of Controller,label=left:Host,label={[label distance=5.5mm]},Dotted,minimum height=48mm](Host){};
\node[above left = 3mm and 6mm of Host.south east,anchor=south east,Normal](L2CAP){Logical Link Control and Adaption Protocol\\(L2CAP)};
\node[above =15mm of L2CAP.north east,anchor=north east,Normal,minimum width=51mm](ATT){Attribute Protocol\\(ATT)};
\node[above = 15mm of ATT.east,anchor=east,Normal,minimum width=51mm](GATT){Generic Attribute Profile\\(GATT)};
\node[above = 15mm of L2CAP.north west,anchor=north west,Normal,minimum width=51mm](GAP){Generic Access Profile\\(GAP)};
\node[above = 15mm of GAP.north west,anchor=north west,Normal,minimum width=51mm](SMP){Security Manager Protocol\\(SMP)};
\node[above = 3mm of Host,label=left:Application,label={[label distance=5.5mm]},Normal,minimum width=120mm](Application){Application\\(APP)};
\end{tikzpicture}
\end{document}
8
  • When I add \draw(current bounding box.south east) rectangle(current bounding box.north west); to the end, I can see about 1 cm white space at the top of the tikz image. How can I reduce this space?
    – Pascal
    Commented Sep 1, 2019 at 6:43
  • You can shift like this \draw(current bounding box.south east) rectangle([shift={(0mm,-8mm)}]current bounding box.north west);
    – AndréC
    Commented Sep 1, 2019 at 7:05
  • Yes, but this is just a hack isn't it? I found out, that the white space is produced through my label definition label=left:Host,label={[label distance=5.5mm]}. If I activate draw labels I can see a label above the Host. Is there a way just to draw the left label and not the label above?
    – Pascal
    Commented Sep 1, 2019 at 7:09
  • It is the syntax of TikZ coordinates that allows you to specify local options as described on page 133 of the 3.1.4b manual. I quote The general syntax is ([<options>]coordinate specification). This allows coordinates to be adjusted locally.
    – AndréC
    Commented Sep 1, 2019 at 7:21
  • You can simplify your code by using styles as Mr. Al Jumaily and Zarko did in their answer. It is still heavy, but it is normal that it is for the first time. Using styles makes it easier to understand the code and maintain it over time.
    – AndréC
    Commented Sep 1, 2019 at 7:35
4

This answer shows the differences between my first try solution in the question and my final solution. The differences are located in the label definitions, please see my comments.

CODE

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}

\begin{document}
% First try mentioned in the question
\begin{tikzpicture}[align=center,>=latex',font=\sffamily]
\tikzstyle{Dotted} = [draw=black,dashed,thick,rectangle,minimum width=120mm,minimum height=33mm]
\tikzstyle{Normal} = [draw=black,thick,rectangle,minimum width=108mm,minimum height=12mm]
\node[label=left:Controller,label={[label distance=5.5mm]},Dotted](Controller){};
\node[above left = 3mm and 6mm of Controller.south east,anchor=south east,Normal](PHY){LE Physical Layer\\(PHY)};
\node[above = 3mm of PHY,Normal](LL){Link Layer\\(LL)};
\node[above = 10mm of Controller.north west,anchor=north west](HCI0){};
\node[above = 10mm of Controller.north east,anchor=north east](HCI1){};
\draw[dash pattern={on 5mm off 3mm},line width=1.2mm,](HCI0) -- (HCI1) node[midway,fill=white]{Host Controller Interface\\(HCI)};
\node[above = 18mm of Controller,label=left:Host,label={[label distance=5.5mm]},Dotted,minimum height=48mm](Host){};
\node[above left = 3mm and 6mm of Host.south east,anchor=south east,Normal](L2CAP){Logical Link Control and Adaption Protocol\\(L2CAP)};
\node[above =15mm of L2CAP.north east,anchor=north east,Normal,minimum width=51mm](ATT){Attribute Protocol\\(ATT)};
\node[above = 15mm of ATT.east,anchor=east,Normal,minimum width=51mm](GATT){Generic Attribute Profile\\(GATT)};
\node[above = 15mm of L2CAP.north west,anchor=north west,Normal,minimum width=51mm](GAP){Generic Access Profile\\(GAP)};
\node[above = 15mm of GAP.north west,anchor=north west,Normal,minimum width=51mm](SMP){Security Manager Protocol\\(SMP)};
\node[above = 3mm of Host,label=left:Application,label={[label distance=5.5mm]},Normal,minimum width=120mm](Application){Application\\(APP)};
% Just to demonstrate the outlines
\draw(current bounding box.south east) rectangle(current bounding box.north west);
\end{tikzpicture}

% Second try with improved outlines
\begin{tikzpicture}[align=center,>=latex',font=\sffamily]
\tikzstyle{Dotted} = [draw=black,dashed,thick,rectangle,minimum width=120mm,minimum height=33mm]
\tikzstyle{Normal} = [draw=black,thick,rectangle,minimum width=108mm,minimum height=12mm]
% Edited label configuration
\node[label={[label distance=3mm]left:Controller},Dotted](Controller){};
\node[above left = 3mm and 6mm of Controller.south east,anchor=south east,Normal](PHY){LE Physical Layer\\(PHY)};
\node[above = 3mm of PHY,Normal](LL){Link Layer\\(LL)};
\node[above = 10mm of Controller.north west,anchor=north west](HCI0){};
\node[above = 10mm of Controller.north east,anchor=north east](HCI1){};
\path[](HCI0) -- (HCI1) node[midway,fill=white](aux){Host Controller Interface\\(HCI)}; 
\draw [dash pattern={on 5mm off 3mm},line width=1.2mm](aux)--(HCI0)(aux)--(HCI1);
% Edited label configuration
\node[above = 18mm of Controller,label={[label distance=3mm]left:Host},Dotted,minimum height=48mm](Host){};
\node[above left = 3mm and 6mm of Host.south east,anchor=south east,Normal](L2CAP){Logical Link Control and Adaption Protocol\\(L2CAP)};
\node[above =15mm of L2CAP.north east,anchor=north east,Normal,minimum width=51mm](ATT){Attribute Protocol\\(ATT)};
\node[above = 15mm of ATT.east,anchor=east,Normal,minimum width=51mm](GATT){Generic Attribute Profile\\(GATT)};
\node[above = 15mm of L2CAP.north west,anchor=north west,Normal,minimum width=51mm](GAP){Generic Access Profile\\(GAP)};
\node[above = 15mm of GAP.north west,anchor=north west,Normal,minimum width=51mm](SMP){Security Manager Protocol\\(SMP)};
% Edited label configuration
\node[above = 3mm of Host,label={[label distance=3mm]left:Application},Normal,minimum width=120mm](Application){Application\\(APP)};
% Just to demonstrate the outlines
\draw(current bounding box.south east) rectangle(current bounding box.north west);
\end{tikzpicture}
\end{document}

RESULT

  • On the left: First try
  • On the right: Final solution

enter image description here

3
  • 1
    Please, explain what id in your answer different from others, especial from accept one. From image i can only conclude, that you add borders around labels (what wasn't demand in your question).
    – Zarko
    Commented Sep 1, 2019 at 8:15
  • Sorry, beside to changed distance of labels Controller, Host and Application I don't see any differences :-). that means that you add new size of label distance only ... you should mentioned this demand at least in comments below received answers ... see my edited answer. BTW, desired changes of received answers in form of last answer is not fer to people who prepare answers according to your question.
    – Zarko
    Commented Sep 1, 2019 at 9:03
  • 1
    I primarily didn't want to answer my question but please see the comment below AndréC's answer. Once you have finalized your code. It is recommended on this site to answer your own question. Read – AndréC
    – Pascal
    Commented Sep 1, 2019 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .