aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/Connection.c
blob: 1d9f43cd19d5c6d27244ba880094287a59d43d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; backgrou
/*
 * Connection.c

 *
 *  Created on: 4 June 2018
 *      Author: shlomo
 */

#include <Container.h>
#include <DataDef.h>
#include <PMR/Common/MessageContainer.pb-c.h>
#include <PMR/Connection/ConnectRequest.pb-c.h>
#include <PMR/Connection/ConnectResponse.pb-c.h>
#include <PMR/Connection/DisConnectRequest.pb-c.h>
#include <PMR/Connection/DisConnectResponse.pb-c.h>
#include <PMR/Connection/KeepAliveRequest.pb-c.h>
#include <PMR/Connection/KeepAliveResponse.pb-c.h>
#include <PMR/Connection/DeviceInformation.pb-c.h>

#include "Common/SW_Info/SW_Info.h"
#include "Common/report/report.h"
#include <utils/ustdlib.h>
#include "drivers/FPGA/FPGA.h"

#include "StateMachines/Printing/PrintingSTM.h"

#include "Modules/heaters/heaters_ex.h"
#include "modules/Diagnostics/Diagnostics.h"
#include "Modules/AlarmHandling/AlarmHandling.h"
#include "Modules/Stubs_Handler/Stub_HW_Version.h"

char HexToDecimal(char hex)
{
    int i = hex % 0x10;
    int j = hex / 0x10;
    return j*10+i;
}
void ConnectionRequest(MessageContainer* requestContainer)
{
    MessageContainer responseContainer;

    ConnectRequest* request = connect_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
    DeviceInformation DevInfo = DEVICE_INFORMATION__INIT;
    ConnectResponse response =   CONNECT_RESPONSE__INIT;

    if (GeneralHwReady == false)
    {
        return;
    }
    unsigned char Major = 0;
    unsigned char Year = 0 ;
    unsigned char Month = 0;
    unsigned char Day = 0;

	char Version[30] = {0};
	char FPGA1Version[30] = {0};
	char FPGA2Version[30] = {0};
	char FPGA3Version[30] = {0};
	char BoardVersion[30] = {0};
	char AssyVersion[30] = {0};
    response.deviceinformation = &DevInfo;

    /*
    extern TangoVersion_t _gTangoVersion;
    extern char Dat[50];
    extern char _gTangoName [MAX_STRING_LEN];//d
      */
    DevInfo.builddate = Dat;
    DevInfo.name = _gTangoName;
    usnprintf(Version, 30, "%d.%d.%d.%d",_gTangoVersion.m_major,_gTangoVersion.m_minor,_gTangoVersion.m_patch,_gTangoVersion.m_build);
    DevInfo.version = Version;

    FPGA_ReadVersion(1, &Major, &Year, &Month, &Day);
    usnprintf(FPGA1Version, 30, "FPGA1: Ver %d %d/%d/20%d",HexToDecimal(Major), HexToDecimal(Day),HexToDecimal(Month),HexToDecimal(Year));
    DevInfo.fpga1version = FPGA1Version;

    FPGA_ReadVersion(2, &Major, &Year, &Month, &Day);
    usnprintf(FPGA2Version, 30, "FPGA2: Ver %d %d/%d/20%d",HexToDecimal(Major), HexToDecimal(Day),HexToDecimal(Month),HexToDecimal(Year));
    DevInfo.fpga2version = FPGA2Version;

    FPGA_ReadVersion(3, &Major, &Year, &Month, &Day);
    usnprintf(FPGA3Version, 30, "FPGA3: Ver %d %d/%d/20%d",HexToDecimal(Major), HexToDecimal(Day),HexToDecimal(Month),HexToDecimal(Year));
    DevInfo.fpga3version = FPGA3Version;

    Read_HW_Version(BoardVersion, AssyVersion);

    DevInfo.boardrevision = BoardVersion;
    DevInfo.assemblyversion = AssyVersion;



    responseContainer = createContainer(MESSAGE_TYPE__ConnectResponse, requestContainer->token, true, &response, &connect_response__pack, &connect_response__get_packed_size);

    //-------------------------------------------------------------------------------------------
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars((char*)container_buffer, container_size);
    connect_request__free_unpacked(request,NULL);
}
void DisconnectionRequest(MessageContainer* requestContainer)
{
    MessageContainer responseContainer;

    //DisconnectRequest* request = disconnect_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
    DisconnectResponse response =   DISCONNECT_RESPONSE__INIT;

    responseContainer = createContainer(MESSAGE_TYPE__DisconnectResponse, requestContainer->token, true, &response, &disconnect_response__pack, &disconnect_response__get_packed_size);
    ReportStopReporting();
    DiagnosticsStop();
    JobStopReporting();
    AlarmHandlingStop();
    //UART_ResetBuffers();
    //-------------------------------------------------------------------------------------------
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars((char*)container_buffer, container_size);
    //disconnect_request__free_unpacked(request,NULL);
    //CommunicationMailboxFlush();
    //USBCDCD_Reinit();

}
int KeepAliveOneSecondCounter = 0;
#define KEEPALIVE_COMMUNICATION_ABORT_LIMIT 30
bool KeepAliveActive = false;
void KeepAliveRequestFunc(MessageContainer* requestContainer)
{
    MessageContainer responseContainer;

    //KeepAliveRequest* request = keep_alive_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
    KeepAliveResponse response =   KEEP_ALIVE_RESPONSE__INIT;

    KeepAliveOneSecondCounter = 0;
    KeepAliveActive = true;
    responseContainer = createContainer(MESSAGE_TYPE__KeepAliveResponse, requestContainer->token, true, &response, &keep_alive_response__pack, &keep_alive_response__get_packed_size);
    //-------------------------------------------------------------------------------------------
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars((char*)container_buffer, container_size);
    //keep_alive_request__free_unpacked(request,NULL);
}
void KeepAliveOneSecondCall(void)
{
    if (KeepAliveActive == false)
        return;
    KeepAliveOneSecondCounter++;
    if (KeepAliveOneSecondCounter > KEEPALIVE_COMMUNICATION_ABORT_LIMIT)
    {
        LOG_ERROR(KeepAliveOneSecondCounter, "Communication keepalive failed");
        KeepAliveOneSecondCounter = 0;
        KeepAliveActive = false;
        ReportStopReporting();
        DiagnosticsStop();
        JobStopReporting();
        AlarmHandlingStop();
        //CommunicationMailboxFlush();
        USBCDCD_Reinit();
        //UART_ResetBuffers();
    }
}