aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/Connection.c
blob: 0eac579aeb38ff042c084d0900715f2fc8ab8c1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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/SWUpdate/FileSystem.h"
#include "Common/SW_Info/SW_Info.h"
#include "Common/report/report.h"
#include "Communication/CommunicationTask.h"
#include <utils/ustdlib.h>
#include "drivers/FPGA/FPGA.h"
#include "Drivers/FPGA/FPGA_GPIO/FPGA_GPIO.h"

#include "StateMachines/Printing/PrintingSTM.h"
#include "StateMachines/Initialization/InitSequence.h"
#include "StateMachines/Initialization/PowerOffSequence.h"
#include "Modules/General/MachineStatus.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"

bool FirstConnection = true;
char HexToDecimal(char hex)
{
    int i = hex % 0x10;
    int j = hex / 0x10;
    return j*10+i;
}
bool KeepAliveActive = false;

void StopRecurringReports(void)
{
    ReportStopReporting();
    DiagnosticsStop();
    JobStopReporting();
    AlarmHandlingStop();
    PowerUpStopReporting();
    MachineUpdateStopReporting();
    PowerDownStopReporting();
    HomingStopReporting();

    KeepAliveActive = false;
}
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;

    if (request->has_unixtime)
        utilsUpdateDateTime(request->unixtime);//(request->seconds);
    StopRecurringReports();
    /*
    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;

    DevInfo.has_diagnosticsinterval = true;
    DevInfo.diagnosticsinterval = GetDiagnosticCollectionLimit();

    response.has_isafterreset = true;
    response.isafterreset = FirstConnection;
    FirstConnection = false;


    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();
    HomingStopReporting();
    //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 10
bool keepalivetest = 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;

    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);
}

int  KARequestId = 0x01010101;
char KARequestToken[36+1];
void KeepAliveResponseFunc(MessageContainer* requestContainer)
{
    //KeepAliveRequest* request =
    KeepAliveResponse *response =   keep_alive_response__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
    if (strcmp(requestContainer->token,KARequestToken)==0)
    {
        if (keepalivetest == false)
            KeepAliveOneSecondCounter = 0;
        KeepAliveActive = true;
        //Report("keepalive response received",__FILE__,__LINE__,KARequestId,RpWarning,msec_millisecondCounter, 0);
    }
    keep_alive_response__free_unpacked(response,NULL);
}

void KeepAliveRequest1Second(void)
{
    MessageContainer responseContainer;

    KeepAliveResponse request =   KEEP_ALIVE_REQUEST__INIT;

    strcpy(KARequestToken,"Happy days are coming to us, soon");
    memcpy(&KARequestToken[32],&KARequestId,sizeof(KARequestId));
    KARequestId++;
    //Report("keepalive request sent",__FILE__,__LINE__,KARequestId,RpWarning,msec_millisecondCounter, 0);

    if (keepalivetest == true)
        return;
    //    KeepAliveOneSecondCounter = 0;
    //KeepAliveActive = true;
    responseContainer = createContainer(MESSAGE_TYPE__KeepAliveRequest, KARequestToken, true, &request, &keep_alive_request__pack, &keep_alive_request__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);
    SendChars((char*)container_buffer, container_size);
}
void KeepAliveOneSecondCall(void)
{
    if (FileUploadGetState()==false)
        return;
    if (FileAnalysisGetState()==true)
        return;
    KeepAliveRequest1Second();
    if (KeepAliveActive == false)
        return;

    KeepAliveOneSecondCounter++;
    if (KeepAliveOneSecondCounter > (KEEPALIVE_COMMUNICATION_ABORT_LIMIT/2))
    {
        Report("keepalive danger",__FILE__,__LINE__,KeepAliveOneSecondCounter,RpWarning,msec_millisecondCounter, 0);
    }
    if (KeepAliveOneSecondCounter > KEEPALIVE_COMMUNICATION_ABORT_LIMIT)
    {
        Report("keepalive ",__FILE__,__LINE__,KeepAliveOneSecondCounter,RpWarning,msec_millisecondCounter, 0);
        //LOG_ERROR(KeepAliveOneSecondCounter, "Communication keepalive failed");
        KeepAliveOneSecondCounter = 0;
        StopRecurringReports();
        CommunicationMailboxFlush();
        keepalivetest = false;
        //USBCDCD_init();
        USBCDCD_Reinit();
        //UART_ResetBuffers();
    }
}