gui_B_mapper.py 54.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# (C) Semantix Information Technologies.
#
# Semantix Information Technologies is licensing the code of the
# Data Modelling Tools (DMT) in the following dual-license mode:
#
# Commercial Developer License:
#       The DMT Commercial Developer License is the suggested version
# to use for the development of proprietary and/or commercial software.
# This version is for developers/companies who do not want to comply
# with the terms of the GNU Lesser General Public License version 2.1.
#
# GNU LGPL v. 2.1:
#       This version of DMT is the one to use for the development of
# applications, when you are willing to comply with the terms of the
# GNU Lesser General Public License version 2.1.
#
# Note that in both cases, there are no charges (royalties) for the
# generated code.
#
21
22
23

# pylint: disable=too-many-lines

24
25
26
import re
import os

27
from typing import Set, IO, Union, Any, List, Dict  # NOQA pylint: disable=unused-import
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
28

29
30
31
from ..commonPy.asnAST import (
    AsnBasicNode, AsnEnumerated, AsnSequence, AsnSet, AsnChoice,
    AsnSequenceOf, AsnSetOf, AsnMetaMember, AsnInt, AsnReal, AsnOctetString,
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
32
    AsnNode, AsnBool, isSequenceVariable, sourceSequenceLimit)
33
34
from ..commonPy.utility import panic, panicWithCallStack
from ..commonPy import asnParser
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
35
36
from ..commonPy.asnParser import AST_Lookup, AST_Leaftypes
from ..commonPy.aadlAST import ApLevelContainer, Param
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
g_HeaderFile: IO[Any]
g_SourceFile: IO[Any]
g_GnuplotFile: IO[Any]

g_MyEvents: IO[Any]
g_MyCreation: IO[Any]
g_MyClickPrototypes: IO[Any]
g_MyAction: IO[Any]
g_MyControls: IO[Any]
g_MyLoad: IO[Any]
g_MySave: IO[Any]
g_MyThreadsInc: IO[Any]
g_MyThreadsH: IO[Any]
g_MyTelemetryActions: IO[Any]
52
53
54
55
56
57

g_bStarted = False
g_IDs = 20000
g_asn_name = ""
g_outputDir = ""
g_maybeFVname = ""
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
58
g_perFV = set()  # type: Set[str]
59
60
61
g_langPerSP = {}


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
62
def CleanName(name: str) -> str:
63
64
65
    return re.sub(r'[^a-zA-Z0-9_]', '_', name)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
66
def WriteSourceFileStart() -> None:
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
166
167
168
169
170
171
172
173
174
175
176
177
178
    g_SourceFile.write('''#include "wx/wxprec.h"
#include "wx/convauto.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "telecmds.h"

#include <iostream>
#include <sstream>
#include <iomanip>

#include <mqueue.h>

#include "debug_messages.h"
#include "queue_manager.h"

IMPLEMENT_DYNAMIC_CLASS( TeleCmds, wxFrame )

BEGIN_EVENT_TABLE( TeleCmds, wxFrame )
#include "MyEvents.inc"
END_EVENT_TABLE()

using namespace std;

template <class T>
bool StringToAny(const char *controlName, const std::string& s, T& result, string& msgError)
{
    if (s.length() == 0) {
        msgError = string(controlName) + string(" is empty!");
        return false;
    }
    std::istringstream str(s);
    str >> result;
    if (str.bad() || !str.eof()) {
        std::ostringstream ostr;
        ostr << "Couldn't convert '" << s << "' to " << controlName << " !\\n";
        msgError = ostr.str();
        return false;
    }
    return true;
};

TeleCmds::TeleCmds()
{
    Init();
}

TeleCmds::TeleCmds( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}

bool TeleCmds::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    wxFrame::Create( parent, id, caption, pos, size, style );

    CreateControls();
    Centre();
    return true;
}

TeleCmds::~TeleCmds()
{
}

void TeleCmds::Init()
{
}

bool TeleCmds::ShowToolTips()
{
    return true;
}

wxBitmap TeleCmds::GetBitmapResource( const wxString& name )
{
    wxUnusedVar(name);
    return wxNullBitmap;
}

wxIcon TeleCmds::GetIconResource( const wxString& name )
{
    wxUnusedVar(name);
    return wxNullIcon;
}

#include "MyThreads.h"
#include "MyThreads.inc"

void TeleCmds::CreateControls()
{
    TeleCmds* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    _itemNotebook3 = new wxNotebook( itemDialog1, ID_NOTEBOOK, wxDefaultPosition, wxSize(400, 300), wxNB_DEFAULT );

#include "MyCreation.inc"

    itemBoxSizer2->Add(_itemNotebook3, 1, wxGROW|wxALL, 5);
}

''')

179

Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
180
enumFieldNames = {}  # type: Dict[str, int]
181
182


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
183
def VerifySingleFieldEnums(node: Union[str, AsnNode]) -> None:
184
    while isinstance(node, str):
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
185
        node = asnParser.g_names[node]
186
187
188
189
190
191
192
193
    if isinstance(node, AsnBasicNode):
        pass
    elif isinstance(node, AsnEnumerated):
        for m in node._members:
            if m[0] not in enumFieldNames or enumFieldNames[m[0]] == id(node):
                enumFieldNames[m[0]] = id(node)
            else:  # pragma: no cover
                panic("ENUMERATED fields must be unique (across all ENUMERATED) for the GUI mapper to work... (%s)" % node.Location())  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
194
    elif isinstance(node, (AsnSequence, AsnSet, AsnChoice)):
195
196
        for x in node._members:
            VerifySingleFieldEnums(x[1])
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
197
    elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
198
199
        VerifySingleFieldEnums(node._containedType)
    elif isinstance(node, AsnMetaMember):
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
200
        VerifySingleFieldEnums(asnParser.g_names[node._containedType])
201
    else:  # pragma: no cover
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
202
203
204
205
206
207
208
209
210
211
212
213
214
        assert not isinstance(node, str)
        panicWithCallStack(
            "unsupported %s (%s)" % (str(node.__class__), node.Location()))  # pragma: no cover


def OneTimeOnly(
        unused_modelingLanguage: str,
        asnFile: str,
        subProgram: ApLevelContainer,
        subProgramImplementation: str,
        outputDir: str,
        maybeFVname: str,
        unused_useOSS: bool) -> None:
215
216
    for typename in asnParser.g_names:
        node = asnParser.g_names[typename]
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
        VerifySingleFieldEnums(node)

    global g_outputDir
    g_outputDir = outputDir
    global g_maybeFVname
    g_maybeFVname = maybeFVname
    global g_HeaderFile
    g_HeaderFile = open(outputDir + 'telecmds.h', 'w')
    global g_GnuplotFile
    g_GnuplotFile = open(outputDir + 'gnuplot', 'w')
    g_HeaderFile.write('''#ifndef __TELECMDS_H__
#define __TELECMDS_H__

#include "wx/notebook.h"

#define ID_TELECMDS 10001
#define ID_NOTEBOOK 10005
#define SYMBOL_TELECMDS_IDNAME ID_TELECMDS
#define SYMBOL_TELECMDS_TITLE _T("TeleCommands")
#define SYMBOL_TELECMDS_SIZE wxSize(800, 600)
#define SYMBOL_TELECMDS_POSITION wxDefaultPosition
#define SYMBOL_TELECMDS_STYLE wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxDIALOG_MODAL|wxTAB_TRAVERSAL
''')
    global g_IDs
241
    g_HeaderFile.write("#define ID_MENU_RI " + str(g_IDs) + "\n")
242
    g_IDs += 1
243
    g_HeaderFile.write("#define ID_MENU_SAVE " + str(g_IDs) + "\n")
244
    g_IDs += 1
245
    g_HeaderFile.write("#define ID_MENU_LOAD " + str(g_IDs) + "\n")
246
    g_IDs += 1
247
    g_HeaderFile.write("#define ID_MENU_ABOUT " + str(g_IDs) + "\n")
248
    g_IDs += 1
249
    g_HeaderFile.write("#define ID_MENU_QUIT " + str(g_IDs) + "\n")
250
    g_IDs += 1
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    g_HeaderFile.write("\n")
    g_HeaderFile.write('''
class TeleCmds : public wxFrame
{
    DECLARE_DYNAMIC_CLASS( TeleCmds )
    DECLARE_EVENT_TABLE()
public:
    TeleCmds();
    TeleCmds( wxWindow* parent, wxWindowID id = SYMBOL_TELECMDS_IDNAME, const wxString& caption = SYMBOL_TELECMDS_TITLE, const wxPoint& pos = SYMBOL_TELECMDS_POSITION, const wxSize& size = SYMBOL_TELECMDS_SIZE, long style = SYMBOL_TELECMDS_STYLE );

    bool Create( wxWindow* parent, wxWindowID id = SYMBOL_TELECMDS_IDNAME, const wxString& caption = SYMBOL_TELECMDS_TITLE, const wxPoint& pos = SYMBOL_TELECMDS_POSITION, const wxSize& size = SYMBOL_TELECMDS_SIZE, long style = SYMBOL_TELECMDS_STYLE );
    ~TeleCmds();
    void Init();
    void CreateControls();

#include "MyClickPrototypes.inc"
    wxBitmap GetBitmapResource( const wxString& name );
    wxIcon GetIconResource( const wxString& name );
    static bool ShowToolTips();

    wxNotebook* _itemNotebook3;
''')
    g_HeaderFile.write('''
#include "MyControls.inc"
};
''')
    global g_MyEvents
278
    g_MyEvents = open(outputDir + "MyEvents.inc", "w")
279
    global g_MyCreation
280
    g_MyCreation = open(outputDir + "MyCreation.inc", "w")
281
    global g_MyClickPrototypes
282
    g_MyClickPrototypes = open(outputDir + "MyClickPrototypes.inc", "w")
283
    global g_MyControls
284
    g_MyControls = open(outputDir + "MyControls.inc", "w")
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    global g_MyLoad
    g_MyLoad = open(g_outputDir + 'MyLoad.inc', 'w')
    global g_MySave
    g_MySave = open(g_outputDir + 'MySave.inc', 'w')
    global g_MyThreadsInc
    g_MyThreadsInc = open(g_outputDir + 'MyThreads.inc', 'w')
    global g_MyThreadsH
    g_MyThreadsH = open(g_outputDir + 'MyThreads.h', 'w')
    global g_MyTelemetryActions
    g_MyTelemetryActions = open(g_outputDir + "MyTelemetryActions.inc", 'w')
    global g_SourceFile
    g_SourceFile = open(outputDir + 'telecmds.cpp', 'w')
    global g_asn_name
    g_asn_name = os.path.basename(os.path.splitext(asnFile)[0])
    g_SourceFile.write("#include \"%s.h\"\n\n" % g_asn_name)
    g_SourceFile.write("#include \"%s_enums_def.h\"\n" % maybeFVname)
    WriteSourceFileStart()
    g_SourceFile.write("\n")
    if maybeFVname == "":
304
        panic("GUI APLCs must have an FV_Name attribute! (%s)\n" % subProgram._id + "." + subProgramImplementation)  # pragma: no cover
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    g_SourceFile.write("#include \"%s_gui_header.h\"\n\n" % maybeFVname)
    g_SourceFile.write("#include \"queue_manager.h\"\n\n")
    g_SourceFile.write("void TeleCmds::OnMenu_Click( wxCommandEvent& event )\n{\n")
    g_SourceFile.write("#include \"MyActions.inc\"\n")
    g_SourceFile.write("}\n\n")
    g_SourceFile.write("void TeleCmds::OnMenu_Save( wxCommandEvent& event )\n{\n")
    g_SourceFile.write("#include \"MySave.inc\"\n")
    g_SourceFile.write("}\n\n")
    g_SourceFile.write("void TeleCmds::OnMenu_Load( wxCommandEvent& event )\n{\n")
    g_SourceFile.write("#include \"MyLoad.inc\"\n")
    g_SourceFile.write("}\n\n")
    g_SourceFile.write("void TeleCmds::OnMenu_About( wxCommandEvent& event )\n{\n")
    g_SourceFile.write("    wxMessageBox(_T(\"This Graphical User Interface (GUI) was automatically created\\nby code generators developed\\nfor the European Space Agency (ESA)\\nby Semantix Information Technologies\\n\\n(C) Semantix Information Technologies 2008\"), _T(\"About Auto-GUI\"), wxICON_INFORMATION);\n")
    g_SourceFile.write("}\n\n")
    g_SourceFile.write("void TeleCmds::OnMenu_Quit( wxCommandEvent& event )\n{\n")
    g_SourceFile.write("    Close(TRUE);\n    exit(0);\n")
    g_SourceFile.write("}\n\n")

    # Prototypes in the class declaration
    g_MyClickPrototypes.write("void OnMenu_Click( wxCommandEvent& event );\n")
    g_MyClickPrototypes.write("void OnMenu_Save( wxCommandEvent& event );\n")
    g_MyClickPrototypes.write("void OnMenu_Load( wxCommandEvent& event );\n")
    g_MyClickPrototypes.write("void OnMenu_About( wxCommandEvent& event );\n")
    g_MyClickPrototypes.write("void OnMenu_Quit( wxCommandEvent& event );\n")

    g_MyCreation.write("wxMenu *menuFile = new wxMenu;\n")
    g_MyCreation.write("menuFile->Append( ID_MENU_LOAD, _T(\"&Load...\") );\n")
    g_MyCreation.write("menuFile->Append( ID_MENU_SAVE, _T(\"&Save as...\") );\n")
    g_MyCreation.write("menuFile->AppendSeparator();\n")
    g_MyCreation.write("menuFile->Append( ID_MENU_ABOUT, _T(\"&About...\") );\n")
    g_MyCreation.write("menuFile->AppendSeparator();\n")
    g_MyCreation.write("menuFile->Append( ID_MENU_QUIT, _T(\"E&xit\") );\n")

    g_MyCreation.write("wxMenu *menuFileRI = new wxMenu;\n")
    g_MyCreation.write("menuFileRI->Append( ID_MENU_RI, _T(\"&Invoke current RI\") );\n")

    g_MyCreation.write("wxMenuBar *menuBar = new wxMenuBar;\n")
    g_MyCreation.write("menuBar->Append( menuFile, _T(\"&File\") );\n")
    g_MyCreation.write("menuBar->Append( menuFileRI, _T(\"&Invoke RIs\") );\n")

    g_MyCreation.write("SetMenuBar( menuBar );\n")

    # Instructions for actions per RI
    global g_MyAction
    g_MyAction = open(outputDir + "MyActions.inc", "w")

    g_MyThreadsH.write("#ifndef __MYTHREADSH__\n")
    g_MyThreadsH.write("#define __MYTHREADSH__\n\n")
    g_MyThreadsH.write("#include \"wx/thread.h\"\n\n")
    g_MyThreadsH.write("#include \"PrintTypesAsASN1.h\"\n\n")


# Called once per RI (i.e. per SUBPROGRAM IMPLEMENTATION)
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
358
359
360
361
362
363
364
365
def OnStartup(
        modelingLanguage: str,
        asnFile: str,
        subProgram: ApLevelContainer,
        subProgramImplementation: str,
        outputDir: str,
        maybeFVname: str,
        useOSS: bool) -> None:
366
    # print modelingLanguage, subProgram, subProgramImplementation, maybeFVname
367
368
369
370
371
372
373
    g_langPerSP[subProgram] = modelingLanguage
    global g_bStarted
    if not g_bStarted:
        g_bStarted = True
        OneTimeOnly(modelingLanguage, asnFile, subProgram, subProgramImplementation, outputDir, maybeFVname, useOSS)
    global g_IDs
    CleanSP = CleanName(subProgram._id)
374
    g_HeaderFile.write("#define ID_SCROLWND_" + CleanSP + " " + str(g_IDs) + "\n")
375
    g_IDs += 1
376
377
378
379
380
381
382
    g_MyEvents.write("    EVT_MENU( ID_MENU_RI, TeleCmds::OnMenu_Click )\n")
    g_MyEvents.write("    EVT_MENU( ID_MENU_LOAD, TeleCmds::OnMenu_Load )\n")
    g_MyEvents.write("    EVT_MENU( ID_MENU_SAVE, TeleCmds::OnMenu_Save )\n")
    g_MyEvents.write("    EVT_MENU( ID_MENU_ABOUT, TeleCmds::OnMenu_About )\n")
    g_MyEvents.write("    EVT_MENU( ID_MENU_QUIT, TeleCmds::OnMenu_Quit )\n")

    # Instructions to create the dialog
383
    g_MyControls.write("wxScrolledWindow* _itemScrolledWindow_%s;\n" % CleanSP)
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    g_MyCreation.write("_itemScrolledWindow_%s = new wxScrolledWindow( _itemNotebook3, ID_SCROLWND_%s, wxDefaultPosition, wxSize(100, 100), wxSUNKEN_BORDER|wxHSCROLL|wxVSCROLL|wxALWAYS_SHOW_SB );\n" % (CleanSP, CleanSP))
    g_MyCreation.write("_itemScrolledWindow_%s->SetScrollbars(1, 1, 0, 0);\n" % CleanSP)
    g_MyCreation.write("wxBoxSizer* itemBoxSizer_%s = new wxBoxSizer(wxVERTICAL);\n" % CleanSP)
    g_MyCreation.write("_itemScrolledWindow_%s->SetSizer(itemBoxSizer_%s);\n\n" %
                       (CleanSP, CleanSP))
#
# deprecated, using buttons
#
#    g_MyCreation.write("wxBoxSizer* itemBoxSizerButtons_%s = new wxBoxSizer(wxHORIZONTAL);\n" % CleanSP)
#    g_MyCreation.write("itemBoxSizer_%s->Add(itemBoxSizerButtons_%s, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);\n" %
#       (CleanSP, CleanSP))
#
#    g_MyCreation.write("wxButton* itemButton_ri_%s = new wxButton( _itemScrolledWindow_%s, ID_BTN_RI_%s, _(\"Invoke %s\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
#       (CleanSP, CleanSP, CleanSP, CleanSP ))
#    g_MyCreation.write("wxButton* itemButton_save_%s = new wxButton( _itemScrolledWindow_%s, ID_BTN_SAVE_%s, _(\"Save...\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
#       (CleanSP, CleanSP, CleanSP ))
#    g_MyCreation.write("wxButton* itemButton_load_%s = new wxButton( _itemScrolledWindow_%s, ID_BTN_LOAD_%s, _(\"Load...\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
#       (CleanSP, CleanSP, CleanSP ))
#    g_MyCreation.write("wxButton* itemButton_quit_%s = new wxButton( _itemScrolledWindow_%s, ID_BTN_QUIT_%s, _(\"Quit\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
#       (CleanSP, CleanSP, CleanSP ))
#    g_MyCreation.write("itemBoxSizerButtons_%s->Add(itemButton_ri_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" % (CleanSP, CleanSP))
#    g_MyCreation.write("itemBoxSizerButtons_%s->Add(itemButton_save_%s, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);\n\n" % (CleanSP, CleanSP))
#    g_MyCreation.write("itemBoxSizerButtons_%s->Add(itemButton_load_%s, 0, wxALIGN_RIGHT|wxALL, 5);\n\n" % (CleanSP, CleanSP))
#    g_MyCreation.write("itemBoxSizerButtons_%s->Add(itemButton_quit_%s, 0, wxALIGN_RIGHT|wxALL, 5);\n\n" % (CleanSP, CleanSP))

    # have we ever seen before the combination of FVname and Language?
410
    if maybeFVname + modelingLanguage.lower() not in g_perFV:
411
        # No, check for things that must be instantiated once per FV+Lang
412
        g_perFV.add(maybeFVname + modelingLanguage.lower())
413
414
415
416
417

        # The first time you see an FV with an sp_impl that is also a GUI_PI, create a thread to poll /FVName_PI_queue
        if modelingLanguage.lower() == "gui_pi":
            # We have telemetry, we need a thread polling the /xyz_PI_queue (xyz: g_maybeFVname)
            cleanFVname = CleanName(g_maybeFVname)
418
            # g_MyThreadsH.write("#include \"%s_GUI_reader.h\"\n" % cleanFVname)
419
420
421
422
423
424
425
426
427
            g_MyThreadsH.write("class %s_telemetry : public wxThread {\n" % cleanFVname)
            g_MyThreadsH.write("public:\n")
            g_MyThreadsH.write("    %s_telemetry(TeleCmds *);\n" % cleanFVname)
            g_MyThreadsH.write("    virtual void *Entry();\n")
            g_MyThreadsH.write("    virtual void OnExit();\n")
            g_MyThreadsH.write("private:\n")
            g_MyThreadsH.write("    mqd_t _queue_id;\n")
            g_MyThreadsH.write("    char QName[1024];\n")
            g_MyThreadsH.write("    TeleCmds *_pFrame;\n")
428
            # g_MyThreadsH.write("    int _queue_was_bad;\n")
429
430
431
432
433
434
435
436
437
            g_MyThreadsH.write("};\n\n")
            g_MyThreadsInc.write("%s_telemetry::%s_telemetry(TeleCmds *pFrame)\n{\n" % (cleanFVname, cleanFVname))
            g_MyThreadsInc.write("    _queue_id = (mqd_t)-1;\n")
            g_MyThreadsInc.write("    _pFrame = pFrame;\n")
            g_MyThreadsInc.write("    sprintf(QName, \"%%d_%s_PI_queue\", geteuid());\n" % g_maybeFVname)
            g_MyThreadsInc.write("    if (0 != open_exchange_queue_for_reading((char*)QName, &_queue_id)) {\n")
            g_MyThreadsInc.write("        cerr << \"Failed to open communication channel with ASSERT binary\" << endl;\n")
            g_MyThreadsInc.write("        exit(1);\n")
            g_MyThreadsInc.write("    }\n")
438
            # g_MyThreadsInc.write("    _queue_was_bad = GUI_%s_reader_initialize();\n" % cleanFVname)
439
440
441
            g_MyThreadsInc.write("}\n\n")
            g_MyThreadsInc.write("void *%s_telemetry::Entry()\n{\n" % cleanFVname)
            g_MyThreadsInc.write("    if (_queue_id == (mqd_t)-1) { cout << \"queue \" << QName << \" does not exist!\\n\"; return NULL; }\n")
442
            # g_MyThreadsInc.write("    if (_queue_was_bad) { cout << \"queue for %s does not exist!\\n\"; return NULL; }\n" % g_maybeFVname)
443
444
445
446
447
448
449
450
            g_MyThreadsInc.write("    struct mq_attr mqstat;\n")
            g_MyThreadsInc.write("    mq_getattr(_queue_id, &mqstat);\n")
            g_MyThreadsInc.write("    void* message_data_received = malloc(mqstat.mq_msgsize);\n")
            g_MyThreadsInc.write("    int message_received_type = -1;\n")
            g_MyThreadsInc.write("    if (!message_data_received) { cout << \"Out of memory in queue Entry\\n\"; }\n")
            g_MyThreadsInc.write("    while(1) {\n")
            g_MyThreadsInc.write("        if (TestDestroy()) break;\n")
            g_MyThreadsInc.write("        message_received_type = -1;\n")
451
            # g_MyThreadsInc.write("        GUI_%s_read_data();\n" % cleanFVname)
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
            g_MyThreadsInc.write("        retrieve_message_from_queue(_queue_id, mqstat.mq_msgsize, message_data_received, &message_received_type);\n")
            g_MyThreadsInc.write("        if (message_received_type != -1) {\n")
            g_MyThreadsInc.write("            //cout << \"Received telemetry of type\" << message_received_type << endl;\n")
            g_MyThreadsInc.write("            switch(message_received_type) {\n")
            g_MyThreadsInc.write("#include \"MyTelemetryActions.inc\"\n")
            g_MyThreadsInc.write("            }\n")
            g_MyThreadsInc.write("        }\n")
            g_MyThreadsInc.write("        wxThread::Sleep(10);\n")
            g_MyThreadsInc.write("    }\n")
            g_MyThreadsInc.write("    return NULL;\n")
            g_MyThreadsInc.write("}\n\n")
            g_MyThreadsInc.write("void %s_telemetry::OnExit()\n{\n" % cleanFVname)
            g_MyThreadsInc.write("}\n\n")
            g_MyCreation.write("wxThread *p_%s = new %s_telemetry(this);\n" % (cleanFVname, cleanFVname))
            g_MyCreation.write("p_%s->Create();\n" % cleanFVname)
            g_MyCreation.write("p_%s->Run();\n" % cleanFVname)

    g_MyCreation.write("_itemScrolledWindow_%s->FitInside();\n" % CleanSP)
    if modelingLanguage.lower() == "gui_pi":
        g_MyCreation.write("_itemNotebook3->AddPage(_itemScrolledWindow_%s, _(\"%s\\n(telemetry)\"));\n\n" %
                           (CleanSP, CleanSP))
    elif modelingLanguage.lower() == "gui_ri":
        g_MyCreation.write("_itemNotebook3->AddPage(_itemScrolledWindow_%s, _(\"%s\\n(telecommand)\"));\n\n" %
                           (CleanSP, CleanSP))

    if modelingLanguage.lower() == "gui_pi":
        g_MyTelemetryActions.write("            case i_%s:\n" % CleanSP)
        g_MyTelemetryActions.write("            {\n")
        g_MyTelemetryActions.write('                long long arrivalTime = getTimeInMilliseconds();\n')
        g_MyTelemetryActions.write("                wxMutexGuiEnter();\n")
        g_MyTelemetryActions.write("                char *pData = (char *)message_data_received;\n")
483
484
        names = asnParser.g_names
        leafTypeDict = asnParser.g_leafTypeDict
485
486
487
488
489
490
491
492
        for param in subProgram._params:
            node = names[param._signal._asnNodename]
            CleanParam = CleanName(param._id)
            CleanASNType = CleanName(param._signal._asnNodename)
            g_MyTelemetryActions.write("                // Read the data for param %s\n" % param._id)
            g_MyTelemetryActions.write("                asn1Scc%s var_%s;\n" % (CleanASNType, CleanParam))
            g_MyTelemetryActions.write("                memcpy(&var_%s, pData, sizeof(var_%s));\n" % (CleanParam, CleanParam))
            g_MyTelemetryActions.write("                pData += sizeof(var_%s);\n" % CleanParam)
493
            CopyDataFromASN1ToDlg(g_MyTelemetryActions, "_pFrame->", "var_" + CleanParam, "%s_%s" %
494
495
496
497
498
499
500
501
502
503
                                  (CleanSP, CleanParam), node, leafTypeDict, names)
            g_MyTelemetryActions.write('                PrintASN1%s("TMDATA: %s::%s", &var_%s);\n' %
                                       (CleanASNType, CleanSP, CleanParam, CleanParam))
            g_MyTelemetryActions.write('                printf("\\n");\n')
        g_MyTelemetryActions.write("                wxMutexGuiLeave();\n")
        g_MyTelemetryActions.write('                cout << "TM %s at " << arrivalTime << endl;\n' % CleanSP)
        g_MyTelemetryActions.write("            }\n")
        g_MyTelemetryActions.write("            break;\n")


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
504
505
506
507
508
509
510
511
512
def WriteCodeForGUIControls(
        prefix: str,
        parentControl: str,
        node: AsnNode,
        subProgram: ApLevelContainer,
        subProgramImplementation: str,
        param: Param,
        leafTypeDict: AST_Leaftypes,
        names: AST_Lookup) -> None:
513
514
515
516
517
    global g_IDs
    CleanSP = CleanName(subProgram._id)
    CleanParam = CleanName(param._id)
    ScrollWnd = "_itemScrolledWindow_%s" % CleanSP
    if prefix == "":
518
        prefix = CleanSP + "::" + CleanParam
519
520
521
    varPrefix = prefix.replace("::", "_")
    txtPrefix = re.sub(r'^.*::', '', prefix)
    # Depending on the type of the node, create the appropriate controls
522
    if isinstance(node, (AsnInt, AsnReal, AsnOctetString)):
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
        # Write a static label before the child
        g_MyCreation.write("wxStaticText* itemStaticText_%s =  new wxStaticText( %s, wxID_STATIC, _(\"%s\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
                           (varPrefix, ScrollWnd, txtPrefix))
        g_MyCreation.write("%s->Add(itemStaticText_%s, 0, wxALIGN_LEFT|wxALL, 5);\n" %
                           (parentControl, varPrefix))
        # Use a simple edit box
        g_HeaderFile.write("#define ID_TEXTCTRL_%s %s\n" %
                           (varPrefix, g_IDs))
        g_IDs += 1
        g_MyControls.write("wxTextCtrl* _itemTextCtrl_%s;\n" % varPrefix)
        g_MyCreation.write("_itemTextCtrl_%s = new wxTextCtrl( %s, ID_TEXTCTRL_%s, _T(\"\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
                           (varPrefix, ScrollWnd, varPrefix))
        g_MyCreation.write("%s->Add(_itemTextCtrl_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" %
                           (parentControl, varPrefix))
    elif isinstance(node, AsnBool):
        # Write a checkbox
        g_HeaderFile.write("#define ID_CHECKBOX_%s %s\n" % (varPrefix, g_IDs))
        g_IDs += 1
        g_MyControls.write("wxCheckBox* _itemCheckBox_%s;\n" % varPrefix)
        g_MyCreation.write("_itemCheckBox_%s = new wxCheckBox( %s, ID_CHECKBOX_%s, _T(\"%s\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
                           (varPrefix, ScrollWnd, varPrefix, txtPrefix))
        g_MyCreation.write("_itemCheckBox_%s->SetValue(false);\n" % varPrefix)
        g_MyCreation.write("%s->Add(_itemCheckBox_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" %
                           (parentControl, varPrefix))
    elif isinstance(node, AsnEnumerated):
        # Write a static label before the child
        g_MyCreation.write("wxStaticText* itemStaticText_%s =  new wxStaticText( %s, wxID_STATIC, _T(\"%s\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
                           (varPrefix, ScrollWnd, txtPrefix))
        g_MyCreation.write("%s->Add(itemStaticText_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" %
                           (parentControl, varPrefix))
        # Write a combo
        g_MyControls.write("wxArrayString _itemChoiceStrings_%s;\n" % varPrefix)
        for enumOption in node._members:
            g_MyCreation.write("_itemChoiceStrings_%s.Add(_T(\"%s\"));\n" %
                               (varPrefix, enumOption[0]))
        g_HeaderFile.write("#define ID_CHOICE_%s %s\n" % (varPrefix, g_IDs))
        g_IDs += 1
        g_MyControls.write("wxChoice* _itemChoice_%s;\n" % varPrefix)
        g_MyCreation.write("_itemChoice_%s = new wxChoice(%s, ID_CHOICE_%s, wxDefaultPosition, wxDefaultSize, _itemChoiceStrings_%s, 0);\n" %
                           (varPrefix, ScrollWnd, varPrefix, varPrefix))
        g_MyCreation.write("%s->Add(_itemChoice_%s, 0, wxALIGN_LEFT|wxALL, 5);\n" %
                           (parentControl, varPrefix))
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
565
    elif isinstance(node, (AsnSequence, AsnChoice, AsnSet)):
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
        # Recurse on the children, but first place them all inside a StaticBoxSizer
        g_MyCreation.write("wxStaticBox* itemStaticBoxSizer_%s_Static = new wxStaticBox(%s, wxID_ANY, _T(\"%s\"));\n" %
                           (varPrefix, ScrollWnd, txtPrefix))
        g_MyControls.write("wxStaticBoxSizer* _itemStaticBoxSizer_%s;\n" % varPrefix)
        g_MyCreation.write("_itemStaticBoxSizer_%s = new wxStaticBoxSizer(itemStaticBoxSizer_%s_Static, wxVERTICAL);\n" %
                           (varPrefix, varPrefix))
        g_MyCreation.write("%s->Add(_itemStaticBoxSizer_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" %
                           (parentControl, varPrefix))
        # If it is a CHOICE, we must allow one to specify which child, so write a combo
        if isinstance(node, AsnChoice):
            g_MyControls.write("wxArrayString _itemChoiceStrings_%s;\n" % varPrefix)
            for child in node._members:
                g_MyCreation.write("_itemChoiceStrings_%s.Add(_T(\"%s\"));\n" %
                                   (varPrefix, child[0]))
            g_HeaderFile.write("#define ID_CHOICE_%s %s\n" % (varPrefix, g_IDs))
            g_IDs += 1
582
            g_MyEvents.write("    EVT_CHOICE( ID_CHOICE_" + varPrefix + ", TeleCmds::UpdateChoice_" + varPrefix + " )\n")
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
            g_MyControls.write("wxChoice* _itemChoice_%s;\n" % varPrefix)
            g_MyCreation.write("_itemChoice_%s = new wxChoice(%s, ID_CHOICE_%s, wxDefaultPosition, wxDefaultSize, _itemChoiceStrings_%s, 0);\n" %
                               (varPrefix, ScrollWnd, varPrefix, varPrefix))
            g_MyCreation.write("%s->Add(_itemChoice_%s, 0, wxALIGN_LEFT|wxALL, 5);\n" %
                               ("_itemStaticBoxSizer_%s" % varPrefix, varPrefix))
        # Recurse on a children
        g_MyCreation.write("// Children of " + str(node) + "\n\n")
        for child in node._members:
            CleanChild = CleanName(child[0])
            g_MyCreation.write("// Child " + child[0] + "\n")
            childType = child[1]
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            control = "_itemStaticBoxSizer_%s" % varPrefix
            WriteCodeForGUIControls(prefix + "::" + CleanChild, control, childType, subProgram, subProgramImplementation, param, leafTypeDict, names)
        if isinstance(node, AsnChoice):
            g_MyCreation.write("wxCommandEvent dum_%s; UpdateChoice_%s(dum_%s);\n" % (varPrefix, varPrefix, varPrefix))
            g_MyClickPrototypes.write("void UpdateChoice_%s(wxCommandEvent& event);\n" % varPrefix)
            g_SourceFile.write("void TeleCmds::UpdateChoice_%s(wxCommandEvent& event)\n{\n" % varPrefix)
            count = 0
603
            for unused_child in node._members:
604
                g_SourceFile.write("    if (%d == _itemChoice_%s->GetCurrentSelection()) {\n" % (count, varPrefix))
605
                g_SourceFile.write("        _itemStaticBoxSizer_%s->Show((size_t)%d);\n" % (varPrefix, count + 1))
606
                oCount = 0
607
                for unused_child2 in node._members:
608
                    if oCount != count:
609
                        g_SourceFile.write("        _itemStaticBoxSizer_%s->Show((size_t)%d);\n" % (varPrefix, oCount + 1))
610
611
612
613
614
615
                    oCount += 1
                g_SourceFile.write("    }\n")
                count += 1
            g_SourceFile.write("    _itemScrolledWindow_%s->FitInside();\n" % CleanSP)
            g_SourceFile.write("}\n\n")
        g_MyCreation.write("// End of SEQUENCE/CHOICE\n\n")
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
616
    elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
617
618
619
620
621
622
623
624
        # Inside a StaticBoxSizer...
        g_MyCreation.write("wxStaticBox* itemStaticBoxSizer_%s_Static = new wxStaticBox(%s, wxID_ANY, _T(\"%s\"));\n" %
                           (varPrefix, ScrollWnd, txtPrefix))
        g_MyControls.write("wxStaticBoxSizer* _itemStaticBoxSizer_%s;\n" % varPrefix)
        g_MyCreation.write("_itemStaticBoxSizer_%s = new wxStaticBoxSizer(itemStaticBoxSizer_%s_Static, wxVERTICAL);\n" %
                           (varPrefix, varPrefix))
        g_MyCreation.write("%s->Add(_itemStaticBoxSizer_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" %
                           (parentControl, varPrefix))
625
        if len(node._range) == 2 and node._range[0] != node._range[1]:
626
627
628
            g_MyCreation.write("wxStaticText* itemStaticTextNoElements_%s =  new wxStaticText( %s, wxID_STATIC, _(\"Number of Elements\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
                               (varPrefix, ScrollWnd))
            g_MyCreation.write("%s->Add(itemStaticTextNoElements_%s, 0, wxALIGN_LEFT|wxALL, 5);\n" %
629
                               ("_itemStaticBoxSizer_%s" % varPrefix, varPrefix))
630
631
632
633
634
635
636
637
638
639
640
641
642
643
            g_MyControls.write("wxTextCtrl* _itemTextCtrl_%s;\n" % varPrefix)
            g_MyCreation.write("_itemTextCtrl_%s = new wxTextCtrl( %s, ID_TEXTCTRL_%s, _T(\"%s\"), wxDefaultPosition, wxDefaultSize, 0 );\n" %
                               (varPrefix, ScrollWnd, varPrefix, str(node._range[0])))
            g_MyCreation.write("%s->Add(_itemTextCtrl_%s, 0, wxALIGN_LEFT|wxALL, 5);\n\n" %
                               ("_itemStaticBoxSizer_%s" % varPrefix, varPrefix))
            g_HeaderFile.write("#define ID_TEXTCTRL_%s %s\n" % (varPrefix, g_IDs))
            g_IDs += 1
        # output controls for each element
        containedNode = node._containedType
        while isinstance(containedNode, str):
            containedNode = names[containedNode]
        control = "_itemStaticBoxSizer_%s" % varPrefix
        for i in range(0, node._range[-1]):
            WriteCodeForGUIControls(
644
                prefix + "::Elem_" + ("%02d" % i),
645
646
647
648
649
                control, containedNode, subProgram, subProgramImplementation, param, leafTypeDict, names)
    else:  # pragma: no cover
        panic("GUI codegen doesn't support this type yet (%s)" % str(node))  # pragma: no cover


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
650
def maybeElseZero(childNo: int) -> str:
651
652
653
654
655
656
    if childNo == 0:
        return ""
    else:
        return "else "


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
657
def CopyDataFromDlgToASN1(f: IO[Any], srcVar: str, destVar: str, node: AsnNode, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
658
    if isinstance(node, (AsnInt, AsnReal)):
659
660
661
662
663
664
665
666
667
        targetType = {"INTEGER": "asn1SccSint", "REAL": "double"}[node._leafType]
        f.write("if (false == StringToAny<%s>(\"%s\", _itemTextCtrl_%s->GetValue().ToAscii().release(), %s, msgError)) {\n" % (targetType, srcVar, srcVar, destVar))
        f.write("    wxMessageBox(wxConvLocal.cMB2WC(msgError.c_str()), _T(\"Data input error\"), wxICON_ERROR);\n")
        f.write("    _itemTextCtrl_%s->SetFocus();\n" % srcVar)
        f.write("    return;\n")
        f.write("}\n")
    elif isinstance(node, AsnOctetString):
        f.write("{\n")
        control = "_itemTextCtrl_" + srcVar + "->GetValue()"
668
669
        f.write("    for(size_t i=0; i<" + control + ".size(); i++)\n")
        f.write("        " + destVar + ".arr[i] = " + control + ".at(i);\n")
670
        if isSequenceVariable(node):
671
            f.write("    " + destVar + ".nCount =  _itemTextCtrl_" + srcVar + "->GetValue().size();\n")
672
673
674
675
676
677
        f.write("}\n")
    elif isinstance(node, AsnBool):
        f.write(destVar + " = (asn1SccUint) (_itemCheckBox_" + srcVar + "->Get3StateValue() == wxCHK_CHECKED);\n")
    elif isinstance(node, AsnEnumerated):
        enumNo = 0
        for enumOption in node._members:
678
679
            f.write(("%sif (_itemChoice_" % maybeElseZero(enumNo)) + srcVar + "->GetCurrentSelection() == " + str(enumNo) + ") {\n")
            f.write("    " + destVar + " = ENUM_asn1Scc" + CleanName(enumOption[0]) + ";\n")
680
681
            f.write("}\n")
            enumNo += 1
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
682
    elif isinstance(node, (AsnSequence, AsnSet)):
683
684
685
686
687
688
689
690
691
692
693
        for child in node._members:
            CleanChild = CleanName(child[0])
            childType = child[1]
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            CopyDataFromDlgToASN1(f, srcVar + "_" + CleanChild, destVar + "." + CleanChild, childType, leafTypeDict, names)
    elif isinstance(node, AsnChoice):
        childNo = 0
        for child in node._members:
            CleanChild = CleanName(child[0])
            childType = child[1]
694
            f.write(("%sif (%d == _itemChoice_" % (maybeElseZero(childNo), childNo)) + srcVar + "->GetCurrentSelection()) {\n")
695
696
697
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            CopyDataFromDlgToASN1(f, srcVar + "_" + CleanChild, destVar + ".u." + CleanChild, childType, leafTypeDict, names)
698
            f.write("    " + destVar + ".kind = CHOICE_" + child[2] + ";\n")
699
700
            f.write("}\n")
            childNo += 1
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
701
    elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
702
703
704
705
706
707
708
709
710
711
        containedNode = node._containedType
        while isinstance(containedNode, str):
            containedNode = names[containedNode]
        if isSequenceVariable(node):
            # Read the number of elements from the edit box
            f.write("if (false == StringToAny<long>(\"%s\", _itemTextCtrl_%s->GetValue().ToAscii().release(), %s.nCount, msgError)) {\n" % (srcVar, srcVar, destVar))
            f.write("    wxMessageBox(wxConvLocal.cMB2WC(msgError.c_str()), _T(\"Data input error\"), wxICON_ERROR);\n")
            f.write("    _itemTextCtrl_%s->SetFocus();\n" % srcVar)
            f.write("    return;\n")
            f.write("}\n")
712
713
714
        # No nCount anymore!
        # else:
        #     f.write(destVar + ".nCount = %s;\n" % str(node._range[-1]))
715
716
        for i in range(0, node._range[-1]):
            if isSequenceVariable(node):
717
                f.write("if (" + destVar + ".nCount>" + str(i) + ") {\n")
718
            CopyDataFromDlgToASN1(f, srcVar + "_Elem_" + ("%02d" % i), destVar + ".arr[" + str(i) + "]", containedNode, leafTypeDict, names)
719
720
721
722
            if isSequenceVariable(node):
                f.write("}\n")


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
723
724
725
726
727
728
729
def CopyDataFromASN1ToDlg(fDesc: IO[Any],
                          prefix: str,
                          srcVar: str,
                          destVar: str,
                          node: AsnNode,
                          leafTypeDict: AST_Leaftypes,
                          names: AST_Lookup,
730
                          bClear: bool = False) -> None:
731
732
733
    if isinstance(node, AsnInt):
        fDesc.write("{\n")
        fDesc.write("    ostringstream s;\n")
734
        fDesc.write("    s << " + srcVar + ";\n")
735
736
737
738
739
740
741
742
        if not bClear:
            fDesc.write("    %s_itemTextCtrl_%s->SetValue(wxString(wxConvLocal.cMB2WC(s.str().c_str())));\n" % (prefix, destVar))
        else:
            fDesc.write("    %s_itemTextCtrl_%s->SetValue(wxString(wxConvLocal.cMB2WC(\"\")));\n" % (prefix, destVar))
        fDesc.write("}\n")
    elif isinstance(node, AsnReal):
        fDesc.write("{\n")
        fDesc.write("    ostringstream s;\n")
743
        fDesc.write("    s << fixed << setprecision(8) <<" + srcVar + ";\n")
744
745
746
747
748
749
        if not bClear:
            fDesc.write("    %s_itemTextCtrl_%s->SetValue(wxString(wxConvLocal.cMB2WC(s.str().c_str())));\n" % (prefix, destVar))
        else:
            fDesc.write("    %s_itemTextCtrl_%s->SetValue(wxString(wxConvLocal.cMB2WC(\"\")));\n" % (prefix, destVar))
        fDesc.write("}\n")
    elif isinstance(node, AsnOctetString):
750
        control = prefix + "_itemTextCtrl_" + destVar
751
752
        limit = sourceSequenceLimit(node, srcVar)
        if not bClear:
753
            fDesc.write(control + "->SetValue(wxString(wxConvLocal.cMB2WC(string((const char *)" + srcVar + ".arr, " + limit + ").c_str())));\n")
754
        else:
755
            fDesc.write(control + "->SetValue(wxString(wxConvLocal.cMB2WC(\"\")));\n")
756
    elif isinstance(node, AsnBool):
757
        control = prefix + "_itemCheckBox_" + destVar
758
        if not bClear:
759
            fDesc.write(control + "->Set3StateValue(" + srcVar + "?wxCHK_CHECKED:wxCHK_UNCHECKED);\n")
760
761
762
763
764
765
        else:
            fDesc.write(control + "->Set3StateValue(wxCHK_UNCHECKED);\n")
    elif isinstance(node, AsnEnumerated):
        enumNo = 0
        if not bClear:
            for enumOption in node._members:
766
767
                fDesc.write(("%sif (" % maybeElseZero(enumNo)) + srcVar + " == ENUM_asn1Scc" + CleanName(enumOption[0]) + ") {\n")
                fDesc.write("    " + prefix + "_itemChoice_" + destVar + "->SetSelection(%d);\n" % enumNo)
768
769
770
                fDesc.write("}\n")
                enumNo += 1
        else:
771
            fDesc.write("    " + prefix + "_itemChoice_" + destVar + "->SetSelection(0);\n")
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
772
    elif isinstance(node, (AsnSequence, AsnSet)):
773
774
775
776
777
778
779
780
781
782
783
784
785
        for child in node._members:
            CleanChild = CleanName(child[0])
            childType = child[1]
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            CopyDataFromASN1ToDlg(fDesc, prefix, srcVar + "." + CleanChild, destVar + "_" + CleanChild, childType, leafTypeDict, names, bClear)
    elif isinstance(node, AsnChoice):
        childNo = 0
        for child in node._members:
            CleanChild = CleanName(child[0])
            childType = child[1]
            fDesc.write("%sif (%s.kind == CHOICE_%s) {\n" %
                        (maybeElseZero(childNo), srcVar, child[2]))
786
            fDesc.write("    " + prefix + "_itemChoice_" + destVar + "->SetSelection(%d);\n" % childNo)
787
            fDesc.write("    wxCommandEvent dum;\n")
788
            fDesc.write("    %sUpdateChoice_" % prefix + destVar + "(dum);\n")
789
790
791
792
793
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            CopyDataFromASN1ToDlg(fDesc, prefix, srcVar + ".u." + CleanChild, destVar + "_" + CleanChild, childType, leafTypeDict, names, bClear)
            fDesc.write("}\n")
            childNo += 1
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
794
    elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
795
796
797
798
799
800
        containedNode = node._containedType
        while isinstance(containedNode, str):
            containedNode = names[containedNode]
        # The decoding code has been executed, so the .nCount member is ready...
        for i in range(0, node._range[-1]):
            if isSequenceVariable(node):
801
                fDesc.write("if (" + str(i) + "<" + srcVar + ".nCount) {\n")
802
                CopyDataFromASN1ToDlg(fDesc, prefix, srcVar + ".arr[" + str(i) + "]", destVar + "_Elem_" + ("%02d" % i), containedNode, leafTypeDict, names, bClear)
803
                fDesc.write("} else {\n")
804
                CopyDataFromASN1ToDlg(fDesc, prefix, srcVar + ".arr[" + str(i) + "]", destVar + "_Elem_" + ("%02d" % i), containedNode, leafTypeDict, names, True)
805
806
                fDesc.write("}\n")
            else:
807
                CopyDataFromASN1ToDlg(fDesc, prefix, srcVar + ".arr[" + str(i) + "]", destVar + "_Elem_" + ("%02d" % i), containedNode, leafTypeDict, names, bClear)
808
809
810
        if isSequenceVariable(node):
            fDesc.write("{\n")
            fDesc.write("    ostringstream s;\n")
811
812
            fDesc.write("    s << " + srcVar + ".nCount;\n")
            fDesc.write("    " + prefix + "_itemTextCtrl_%s->SetValue(wxString(wxConvLocal.cMB2WC(s.str().c_str())));\n" % destVar)
813
814
815
            fDesc.write("}\n")


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
816
817
818
819
820
821
822
def WriteCodeForSave(nodeTypename: str,
                     node: AsnNode,
                     subProgram: ApLevelContainer,
                     unused_subProgramImplementation: str,
                     param: Param,
                     leafTypeDict: AST_Leaftypes,
                     names: AST_Lookup) -> None:
823
824
    CleanSP = CleanName(subProgram._id)
    CleanParam = CleanName(param._id)
825
    CopyDataFromDlgToASN1(g_MySave, "%s_%s" % (CleanSP, CleanParam), "var_" + CleanParam, node, leafTypeDict, names)
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
    g_MySave.write("{\n    int ErrCode;\n")
    g_MySave.write("    if (0 == asn1Scc%s_IsConstraintValid(&var_%s, &ErrCode)) {\n" % (CleanName(nodeTypename), CleanParam))
    g_MySave.write("        string msg(\"ASN.1 Constraints are violated with this message!\\n\");\n")
    g_MySave.write("#include \"ConstraintErrors.inc\"\n")
    g_MySave.write("        wxMessageBox(wxConvLocal.cMB2WC(msg.c_str()), _T(\"Data input error\"), wxICON_ERROR);\n")
    g_MySave.write("    } else {\n")
    g_MySave.write("        wxFileDialog flSave(this, _T(\"Choose a file for parameter %s\"), _T(\"\"), _T(\"\"), _T(\"*.per\"), wxFD_SAVE);\n" % param._id)
    g_MySave.write("        if (flSave.ShowModal() == wxID_OK) {\n")
    g_MySave.write("            int errorCode;\n")
    g_MySave.write("            static BitStream strm;\n\n")
    g_MySave.write("            static unsigned char pBuffer[asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING];\n\n" %
                   CleanName(nodeTypename))
    g_MySave.write("            BitStream_Init(&strm, pBuffer, asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING);\n" %
                   CleanName(nodeTypename))
    g_MySave.write("            if (asn1Scc%s_Encode(&var_%s, &strm, &errorCode, TRUE) == FALSE) {\n" %
                   (CleanName(nodeTypename), CleanParam))
842
    g_MySave.write('                wxMessageBox(_T("Encoding of %s failed"), _T("Error encoding"), wxICON_ERROR);\n' % nodeTypename)
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
    g_MySave.write("\t\treturn;\n")
    g_MySave.write("            } else {\n")
    g_MySave.write("                FILE *fp = fopen(flSave.GetPath().ToAscii().release(), \"wb\");\n")
    g_MySave.write("                if(fp) {\n")
    g_MySave.write("                    fwrite(pBuffer, 1, BitStream_GetLength(&strm), fp);\n")
    g_MySave.write("                    fclose(fp);\n")
    g_MySave.write("                } else {\n")
    g_MySave.write("                    wxMessageBox(_T(\"Could not open file for writing...\"), _T(\"Error in saving\"), wxICON_ERROR);\n")
    g_MySave.write("                }\n")
    g_MySave.write("           }\n")
    g_MySave.write("        } else return;\n")
    g_MySave.write("    }\n")
    g_MySave.write("}\n")


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
858
859
860
861
862
863
864
def WriteCodeForLoad(nodeTypename: str,
                     node: AsnNode,
                     subProgram: ApLevelContainer,
                     unused_subProgramImplementation: str,
                     param: Param,
                     leafTypeDict: AST_Leaftypes,
                     names: AST_Lookup) -> None:
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
    CleanSP = CleanName(subProgram._id)
    CleanParam = CleanName(param._id)
    g_MyLoad.write("    {\n")
    g_MyLoad.write("        wxFileDialog flLoad(this, _T(\"Choose a file for parameter %s\"), _T(\"\"), _T(\"\"), _T(\"*.per\"), wxFD_OPEN);\n" % param._id)
    g_MyLoad.write("        if (flLoad.ShowModal() == wxID_OK) {\n")
    g_MyLoad.write("            int errorCode;\n\n")
    g_MyLoad.write("            static BitStream strm;\n\n")
    g_MyLoad.write("            static unsigned char pBuffer[asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING];\n\n" %
                   CleanName(nodeTypename))
    g_MyLoad.write("            FILE *fp = fopen(flLoad.GetPath().ToAscii().release(), \"rb\");\n")
    g_MyLoad.write("            if (fp) {\n")
    g_MyLoad.write("                int iBufferSize = fread(pBuffer, 1, asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING, fp);\n" %
                   CleanName(nodeTypename))
    g_MyLoad.write("                fclose(fp);\n")
    g_MyLoad.write("                BitStream_AttachBuffer(&strm, pBuffer, iBufferSize);\n\n")
    g_MyLoad.write("                if (asn1Scc%s_Decode(&var_%s, &strm, &errorCode)) {\n" %
                   (CleanName(nodeTypename), CleanParam))
    g_MyLoad.write("                    /* Decoding succeeded */\n")
883
    CopyDataFromASN1ToDlg(g_MyLoad, "", "var_" + CleanParam, "%s_%s" % (CleanSP, CleanParam), node, leafTypeDict, names)
884
885
886
887
888
889
890
891
892
893
    g_MyLoad.write("                } else {\n")
    g_MyLoad.write("                    wxMessageBox(_T(\"This file did not contain a %s...\"), _T(\"Error in loading\"), wxICON_ERROR);\n" % nodeTypename)
    g_MyLoad.write("                }\n")
    g_MyLoad.write("            } else {\n")
    g_MyLoad.write("                wxMessageBox(_T(\"Could not open file for reading...\"), _T(\"Error in loading\"), wxICON_ERROR);\n")
    g_MyLoad.write("            }\n")
    g_MyLoad.write("        } else return;\n")
    g_MyLoad.write("    }\n")


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
894
def WriteCodeForGnuPlot(prefix: str, node: AsnNode, subProgram: ApLevelContainer, param: Param, names: AST_Lookup) -> None:
895
896
897
    CleanSP = CleanName(subProgram._id)
    CleanParam = CleanName(param._id)
    if prefix in ("TCDATA: ", "TMDATA: "):
898
        prefix += CleanSP + "::" + CleanParam
899
    if isinstance(node, (AsnInt, AsnReal, AsnOctetString)):
900
901
902
903
904
        g_GnuplotFile.write(prefix + '\n')
    elif isinstance(node, AsnBool):
        pass
    elif isinstance(node, AsnEnumerated):
        pass
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
905
    elif isinstance(node, (AsnSequence, AsnSet)):
906
907
908
909
910
911
912
913
914
915
916
917
918
        for child in node._members:
            CleanChild = CleanName(child[0])
            childType = child[1]
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            WriteCodeForGnuPlot(prefix + "::" + CleanChild, childType, subProgram, param, names)
    elif isinstance(node, AsnChoice):
        for child in node._members:
            CleanChild = CleanName(child[0])
            childType = child[1]
            if isinstance(childType, AsnMetaMember):
                childType = names[childType._containedType]
            WriteCodeForGnuPlot(prefix + "::" + CleanChild, childType, subProgram, param, names)
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
919
    elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
920
921
922
923
924
925
926
        containedNode = node._containedType
        while isinstance(containedNode, str):
            containedNode = names[containedNode]
        for _ in range(0, node._range[-1]):
            WriteCodeForGnuPlot(prefix + "::Elem", containedNode, subProgram, param, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
927
928
929
930
931
932
933
def WriteCodeForAction(nodeTypename: str,
                       node: AsnNode,
                       subProgram: ApLevelContainer,
                       unused_subProgramImplementation: str,
                       param: Param,
                       leafTypeDict: AST_Leaftypes,
                       names: AST_Lookup) -> None:
934
935
    CleanSP = CleanName(subProgram._id)
    CleanParam = CleanName(param._id)
936
    CopyDataFromDlgToASN1(g_MyAction, "%s_%s" % (CleanSP, CleanParam), "var_" + CleanParam, node, leafTypeDict, names)
937
938
939
940
941
942
943
944
945
946
947
948
949
950
    g_MyAction.write("{\n    int ErrCode;\n")
    g_MyAction.write("    if (0 == asn1Scc%s_IsConstraintValid(&var_%s, &ErrCode)) {\n" % (CleanName(nodeTypename), CleanParam))
    g_MyAction.write("        string msg(\"ASN.1 Constraints are violated with this message!\\n\");\n")
    g_MyAction.write("#include \"ConstraintErrors.inc\"\n")
    g_MyAction.write("        wxMessageBox(wxConvLocal.cMB2WC(msg.c_str()), _T(\"Data input error\"), wxICON_ERROR);\n")
    g_MyAction.write("    } else {\n")
    g_MyAction.write("        int errorCode;\n")
    g_MyAction.write("        static BitStream strm;\n\n")
    g_MyAction.write("        static unsigned char pBuffer[asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING];\n\n" %
                     CleanName(nodeTypename))
    g_MyAction.write("        BitStream_Init(&strm, pBuffer, asn1Scc%s_REQUIRED_BYTES_FOR_ENCODING);\n" %
                     CleanName(nodeTypename))
    g_MyAction.write("        if (asn1Scc%s_Encode(&var_%s, &strm, &errorCode, TRUE) == FALSE) {\n" %
                     (CleanName(nodeTypename), CleanParam))
951
    g_MyAction.write('            wxMessageBox(_T("Encoding of %s failed... Contact Semantix"), _T("Error encoding"), wxICON_ERROR);\n' % nodeTypename)
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
    g_MyAction.write("\t\treturn;\n")
    g_MyAction.write("        } else {\n")
    WriteCodeForGnuPlot("TCDATA: ", node, subProgram, param, names)
    g_MyAction.write('            PrintASN1%s("TCDATA: %s::%s", &var_%s);' % (CleanName(nodeTypename), CleanSP, CleanParam, CleanParam))
    g_MyAction.write('            printf("\\n");\n')
    g_MyAction.write("            T_%s_message data;\n" % CleanSP)
    g_MyAction.write("            data.message_identifier = i_%s;\n" % CleanSP)
    g_MyAction.write("            data.message.%s = var_%s;\n" % (CleanParam, CleanParam))
    g_MyAction.write("            static char QName[1024];\n")
    g_MyAction.write("            sprintf(QName, \"/%%d_%s_RI_queue\", geteuid());\n" % g_maybeFVname)
    g_MyAction.write("            mqd_t q = mq_open(QName, O_RDWR | O_NONBLOCK);\n")
    g_MyAction.write("            if (((mqd_t)-1) == q) {\n")
    g_MyAction.write("                wxMessageBox(_T(\"Failed to write message to queue\"), _T(\"Invoking RI failed...\"), wxICON_ERROR);\n")
    g_MyAction.write("            } else {\n")
    g_MyAction.write("                if (0 != write_message_to_queue(q, sizeof(data.message), &data.message, data.message_identifier)) {\n")
    g_MyAction.write('                    fprintf(stderr, "Sending the TC failed...\\n");\n')
    g_MyAction.write('                } else {\n')
    g_MyAction.write('                     cout << "TC %s at " << getTimeInMilliseconds() << endl;\n' % CleanSP)
    g_MyAction.write('                }\n')
    g_MyAction.write("            }\n")
    g_MyAction.write("        }\n")
    g_MyAction.write("    }\n")
    g_MyAction.write("}\n")

976

Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
977
g_SPs = []  # type: List[str]
978
979
980
g_bBraceOpen = False


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
981
982
983
984
985
986
987
def Common(nodeTypename: str,
           node: AsnNode,
           subProgram: ApLevelContainer,
           subProgramImplementation: str,
           param: Param,
           leafTypeDict: AST_Leaftypes,
           names: AST_Lookup) -> None:
988
989
990
    control = "itemBoxSizer_%s" % CleanName(subProgram._id)
    WriteCodeForGUIControls('', control, node, subProgram, subProgramImplementation, param, leafTypeDict, names)
    global g_bBraceOpen
991
    if not g_SPs or subProgram._id != g_SPs[-1]:
992
993
994
995
996
997
998
        if g_bBraceOpen:
            g_MyAction.write("} // %s\n" % g_SPs[-1])
            g_MySave.write("} // %s\n" % g_SPs[-1])
            g_MyLoad.write("} // %s\n" % g_SPs[-1])
            g_bBraceOpen = False
        g_SPs.append(subProgram._id)
        if g_langPerSP[subProgram].lower() == "gui_ri":
999
            g_MyAction.write("if ((int)_itemNotebook3->GetSelection() == %d) {\n" % (len(g_SPs) - 1))
1000
            g_MyAction.write("    string msgError;\n")
1001
            g_MySave.write("if ((int)_itemNotebook3->GetSelection() == %d) {\n" % (len(g_SPs) - 1))
1002
            g_MySave.write("    string msgError;\n")
1003
            g_MyLoad.write("if ((int)_itemNotebook3->GetSelection() == %d) {\n" % (len(g_SPs) - 1))
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
            g_MyLoad.write("    string msgError;\n")
            g_bBraceOpen = True

    if g_langPerSP[subProgram].lower() == "gui_ri":
        g_MyAction.write("    asn1Scc%s var_%s;\n" % (CleanName(nodeTypename), CleanName(param._id)))
        g_MySave.write("    asn1Scc%s var_%s;\n" % (CleanName(nodeTypename), CleanName(param._id)))
        g_MyLoad.write("    asn1Scc%s var_%s;\n" % (CleanName(nodeTypename), CleanName(param._id)))
        WriteCodeForAction(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)
        WriteCodeForSave(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)
        WriteCodeForLoad(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)
    if g_langPerSP[subProgram].lower() == "gui_pi":
        WriteCodeForGnuPlot("TMDATA: ", node, subProgram, param, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1018
def OnBasic(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1019
1020
1021
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1022
def OnSequence(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1023
1024
1025
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1026
def OnSet(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1027
1028
1029
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)  # pragma: nocover


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1030
def OnEnumerated(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1031
1032
1033
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1034
def OnSequenceOf(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1035
1036
1037
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1038
def OnSetOf(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1039
1040
1041
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)  # pragma: nocover


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1042
def OnChoice(nodeTypename: str, node: AsnNode, subProgram: ApLevelContainer, subProgramImplementation: str, param: Param, leafTypeDict: AST_Leaftypes, names: AST_Lookup) -> None:
1043
1044
1045
    Common(nodeTypename, node, subProgram, subProgramImplementation, param, leafTypeDict, names)


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1046
def OnShutdown(unused_modelingLanguage: str, unused_asnFile: str, unused_sp: ApLevelContainer, unused_subProgramImplementation: str, unused_maybeFVname: str) -> None:
1047
1048
1049
    pass


Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1050
def OnFinal() -> None:
1051
1052
1053
1054
1055
1056
    if g_bBraceOpen:
        g_MyAction.write("}\n")
        g_MySave.write("}\n")
        g_MyLoad.write("}\n")
    g_HeaderFile.write("\n#endif\n")
    g_MyThreadsH.write("\n#endif\n")