summaryrefslogtreecommitdiffstats
path: root/Src/Network/IndustrialStack_LLD.cpp
blob: e141aebc8cbbef7c2edc615f49c3ca3af3eb5d4f (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
241
242
243
244
245
246
247
248
249
250
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
278
/*
 * Video On Demand Samples
 *
 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You may also obtain this software under a propriety license from Microchip.
 * Please contact Microchip for further information.
 *
 */

//#define LLD_TRACE

/*----------------------------------------------------------*/
/*! \file                                                   */
/*! \brief   Base Board initialisation                      */
/*----------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <assert.h>

#include "Console.h"
#include "Board.h"
#include "DriverConfiguration.h"
#include "IndustrialStack_LLD.h"

/*----------------------------------------------------------*/
/* Static C helper functions                                */
/*----------------------------------------------------------*/

static void Queue_Init( Queue_t *queue )
{
    assert(NULL != queue);
    queue->testPattern = QUEUE_TESTPATTERN;
    queue->rxPos = 0;
    queue->txPos = 0;
    queue->pRx = queue->dataQueue;
    queue->pTx = queue->dataQueue;
}

static QueueEntry_t *Queue_GetRxPtr( Queue_t *queue )
{
    assert(NULL != queue && QUEUE_TESTPATTERN == queue->testPattern);
    QueueEntry_t *pEntry = NULL;
    if( queue->txPos - queue->rxPos > 0 )
    {
        pEntry = queue->pRx;
    }
    return pEntry;
}

static void Queue_PopRx( Queue_t *queue )
{
    assert(NULL != queue && QUEUE_TESTPATTERN == queue->testPattern);
    if( ++queue->pRx >= queue->dataQueue + BOARD_PMS_RX_QUEUE )
        queue->pRx = queue->dataQueue;
    ++queue->rxPos;
}

static QueueEntry_t *Queue_GetTxPtr( Queue_t *queue )
{
    assert(NULL != queue && QUEUE_TESTPATTERN == queue->testPattern);
    QueueEntry_t *pEntry = NULL;
    if( ( uint32_t )BOARD_PMS_RX_QUEUE + queue->rxPos - queue->txPos > 0 )
    {
        pEntry = queue->pTx;
    }
    return pEntry;
}

static void Queue_PopTx( Queue_t *queue )
{
    assert(NULL != queue && QUEUE_TESTPATTERN == queue->testPattern);
    if( ++queue->pTx >= queue->dataQueue + BOARD_PMS_RX_QUEUE )
        queue->pTx = queue->dataQueue;
    ++queue->txPos;
}

static void *ReceiveThread( void *tag )
{
    assert(NULL != tag);
    CIndustrialStackLld *var = ( CIndustrialStackLld * )tag;
    while( var->allowThreadRun )
    {
        if( -1 == var->hControlRx )
        {
            ConsolePrintf( PRIO_ERROR, RED"File handle for Control-RX is invalid, stopping reader thread."RESETCOLOR"\n" );
            if (NULL != var->listener)
                var->listener->OnReadThreadEnd(var);
            pthread_exit( NULL );
        }
        QueueEntry_t *pEntry = Queue_GetTxPtr(&var->rxQueue);
        if( NULL != pEntry )
        {
            int16_t payloadLen = 0;
            payloadLen = read( var->hControlRx, pEntry->buffer, sizeof( pEntry->buffer ) );
            if( payloadLen < 0 )
            {
                if( var->allowThreadRun )
                {
                    ConsolePrintf( PRIO_ERROR, RED"Stopping reader thread, because of error: '%s'"RESETCOLOR"\n",
                    GetErrnoString() );
                    if (NULL != var->listener)
                        var->listener->OnReadThreadEnd(var);
                }
                pthread_exit( NULL );
            }
            else if( payloadLen != 0 )
            {
                pEntry->payloadLen = payloadLen;
                Queue_PopTx(&var->rxQueue);
#ifdef LLD_TRACE
                {
                    ConsolePrintfStart( PRIO_HIGH, BLUE"%04d: MSG_RX: ", GetTickCountWord());
                    for ( int16_t i = 0; i < pEntry->payloadLen; i++ )
                    {
                        ConsolePrintfContinue( "%02X ", pEntry->buffer[i] );
                    }
                    ConsolePrintfExit(RESETCOLOR"\n");
                }
#endif
            }
        }
        else
        {
            ConsolePrintf( PRIO_ERROR, RED"WARNING, RX QUEUE FULL !!\nPlease increase BOARD_PMS_RX_QUEUE value, or increase polling speed\n"RESETCOLOR"\n" );
            usleep( 10000 );
        }
    }
    ConsolePrintf( PRIO_LOW, "Control Receive Thread ends\n" );
    if( var->allowThreadRun && NULL != var->listener)
        var->listener->OnReadThreadEnd(var);
    pthread_exit( NULL );
}

static void *SendThread( void *tag )
{
    assert(NULL != tag);
    CIndustrialStackLld *var = ( CIndustrialStackLld * )tag;
    while( var->allowThreadRun )
    {
        sem_wait( &var->txSem );
        if (!var->allowThreadRun)
            pthread_exit( NULL );
        QueueEntry_t *pEntry = Queue_GetRxPtr( &var->txQueue );
        assert(NULL != pEntry);
        if( -1 == var->hControlTx )
        {
            ConsolePrintf( PRIO_ERROR, RED"File handle for Control-TX is invalid, stopping send thread."RESETCOLOR"\n" );
            pthread_exit( NULL );
        }
#ifdef LLD_TRACE
        {
            uint32_t i;
            ConsolePrintfStart( PRIO_MEDIUM, YELLOW"%04d: MSG_TX: ", GetTickCountWord());
            for ( i = 0; i < pEntry->payloadLen; i++ )
            {
                ConsolePrintfContinue( "%02X ", pEntry->buffer[i] );
            }
            ConsolePrintfExit(RESETCOLOR"\n");
        }
#endif
        if( write( var->hControlTx, pEntry->buffer, pEntry->payloadLen ) != pEntry->payloadLen )
        {
            ConsolePrintf( PRIO_ERROR, RED"Failed to send %d bytes to the MOST control channel"RESETCOLOR"\n", pEntry->payloadLen );
            usleep(1000);
        }
        else
        {
            Queue_PopRx(&var->txQueue);
        }
        
    }
    pthread_exit( NULL );
}

/*----------------------------------------------------------*/
/* Public method implementations                            */
/*----------------------------------------------------------*/

CIndustrialStackLld::CIndustrialStackLld( int controlRxHandle, int controlTxHandle ) 
    : hControlRx(controlRxHandle), hControlTx(controlTxHandle), allowThreadRun(true), listener(NULL)
{
    Queue_Init(&rxQueue);
    Queue_Init(&txQueue);
    
    if (sem_init(&txSem, 0, 0) == -1)
    {
        ConsolePrintf(PRIO_ERROR, RED"CIndustrialStackLld constructor: sem_init failed"RESETCOLOR"\n");
        return;
    }
    
    pthread_create( &rxThread, NULL, ReceiveThread, this );
    pthread_create( &txThread, NULL, SendThread, this );
}

CIndustrialStackLld::~CIndustrialStackLld()
{
    void *dummy;
    allowThreadRun = false;
    sem_post( &txSem );
    
    pthread_join( txThread, &dummy );
    pthread_join( rxThread, &dummy );
    
    sem_destroy( &txSem );
}

void CIndustrialStackLld::ClearQueues()
{
    ConsolePrintf( PRIO_LOW, "Clearing INIC queues\n" );
    QueueEntry_t *pEntry;
    while( NULL != ( pEntry = Queue_GetRxPtr(&rxQueue) ) )
        Queue_PopRx(&rxQueue);
    while( NULL != ( pEntry = Queue_GetRxPtr(&txQueue) ) )
        Queue_PopRx(&txQueue);
}

uint16_t CIndustrialStackLld::DataAvailable()
{
    uint16_t readLen = 0;
    QueueEntry_t *pEntry = Queue_GetRxPtr(&rxQueue);
    if( NULL != pEntry )
        readLen = pEntry->payloadLen;
    return readLen;
}

uint16_t CIndustrialStackLld::Read( uint8_t *pData, uint32_t bufferLen )
{
    uint16_t readLen = 0;
    QueueEntry_t *pEntry = Queue_GetRxPtr(&rxQueue);
    if( NULL != pData && NULL != pEntry )
    {
        readLen = pEntry->payloadLen;
        if (readLen > bufferLen)
        {
            ConsolePrintf(PRIO_ERROR, RED"CIndustrialStackLld::Read buffer"\
                " length is too small"RESETCOLOR"\n");
            readLen = bufferLen;
        }
        memcpy( pData, pEntry->buffer, readLen );
        Queue_PopRx(&rxQueue);
    }
    return readLen;
}

bool CIndustrialStackLld::Write( uint16_t wLen, uint8_t *pData )
{
    if( -1 == hControlTx )
        return false;
    QueueEntry_t *pEntry = Queue_GetTxPtr( &txQueue );
    if( NULL != pData && NULL != pEntry )
    {
        memcpy( pEntry->buffer, pData, wLen );
        pEntry->payloadLen = wLen;
        Queue_PopTx( &txQueue );
        sem_post( &txSem );
        return true;
    }
    return false;
}
lass="p">\ clang_base_path_target="${STAGING_DIR_TARGET}/usr" \ custom_toolchain="//build/toolchain/cros:target" \ host_toolchain="//build/toolchain/cros:host" \ v8_snapshot_toolchain="//build/toolchain/cros:v8_snapshot" \ target_cpu="${@gn_target_arch_name(d)}" \ use_v8_context_snapshot=false \ custom_toolchain="//build/toolchain/yocto:yocto_target" \ host_toolchain="//build/toolchain/yocto:yocto_native" \ v8_snapshot_toolchain="//build/toolchain/yocto:yocto_target" \ ' PACKAGECONFIG ??= "upower use-egl" PACKAGECONFIG[use-egl] = ",,virtual/egl virtual/libgles2" PACKAGECONFIG[upower] = ",,,upower" GN_DEFINES:append = ' \ ${PACKAGECONFIG_CONFARGS} \ ' python do_write_toolchain_file () { """Writes a BUILD.gn file for Yocto detailing its toolchains.""" toolchain_dir = d.expand("${S}/build/toolchain/yocto") bb.utils.mkdirhier(toolchain_dir) toolchain_file = os.path.join(toolchain_dir, "BUILD.gn") write_toolchain_file(d, toolchain_file) } addtask write_toolchain_file after do_patch before do_configure # V8's JIT infrastructure requires binaries such as mksnapshot and # mkpeephole to be run in the host during the build. However, these # binaries must have the same bit-width as the target (e.g. a x86_64 # host targeting ARMv6 needs to produce a 32-bit binary). Instead of # depending on a third Yocto toolchain, we just build those binaries # for the target and run them on the host with QEMU. python do_create_v8_qemu_wrapper () { """Creates a small wrapper that invokes QEMU to run some target V8 binaries on the host.""" qemu_libdirs = [d.expand('${STAGING_DIR_HOST}${libdir}'), d.expand('${STAGING_DIR_HOST}${base_libdir}')] qemu_cmd = qemu_wrapper_cmdline(d, d.getVar('STAGING_DIR_HOST', True), qemu_libdirs) wrapper_path = d.expand('${OUT_PATH}/v8-qemu-wrapper.sh') with open(wrapper_path, 'w') as wrapper_file: wrapper_file.write("""#!/bin/sh # This file has been generated automatically. # It invokes QEMU to run binaries built for the target in the host during the # build process. %s "$@" """ % qemu_cmd) os.chmod(wrapper_path, 0o755) } do_create_v8_qemu_wrapper[dirs] = "${OUT_PATH}" addtask create_v8_qemu_wrapper after do_patch before do_configure do_configure () { bbnote "do_configure:" bbnote "Base out path: ${B}" export DEPOT_TOOLS_UPDATE=0 export GCLIENT_PY3=1 export PATH="${DEPOT_TOOLS_DIR}:$PATH" export GN_DEFINES="${GN_DEFINES}" export SSL_CERT_DIR="$OECORE_NATIVE_SYSROOT/etc/ssl/certs/" cd ${S} python3 ./build/linux/unbundle/replace_gn_files.py --system-libraries ${GN_UNBUNDLE_LIBS} # Download a few dependencies. Check the current chromium DEPS file when # upgrading to a new milestone. python3 third_party/depot_tools/download_from_google_storage.py --no_resume --extract --no_auth --bucket chromium-fonts -s third_party/test_fonts/test_fonts.tar.gz.sha1 python3 third_party/depot_tools/download_from_google_storage.py --no_resume --extract --no_auth --bucket chromium-nodejs/16.13.0 -s third_party/node/linux/node-linux-x64.tar.gz.sha1 python3 tools/rust/update_rust.py cd ${S}/cef python3 tools/gclient_hook.py --base-out-path ${B} --bypass-sysroot-check } do_compile[progress] = "outof:^\[(\d+)/(\d+)\]\s+" do_compile () { if [ ! -f ${OUT_PATH}/build.ninja ]; then do_configure fi export PATH="${DEPOT_TOOLS_DIR}:$PATH" export PATH="$PATH:${S}/third_party/ninja" ninja ${PARALLEL_MAKE} -C ${OUT_PATH} libcef chrome_sandbox } do_install () { cd ${S}/cef python3 tools/make_distrib.py --output-dir ${OUT_PATH}/dist \ --dist-path-name cef-minimal \ --base-out-path ${B} \ --no-docs \ --no-symbols \ --no-archive \ --ninja-build \ --minimal \ --${GN_TARGET_ARCH_NAME}-build \ --ozone install -d ${D}${CEF_DATA_PATH} cp -R --no-dereference --preserve=mode,links -v ${DIST_PATH}/* ${D}${CEF_DATA_PATH} # TODO(rzanoni): Follow the wiki instructions to install the sandbox } # TODO: fix QA issues, libraries in the wrong location FILES:${PN} += " \ ${CEF_DATA_PATH} \ " INSANE_SKIP:${PN} += "libdir" PROVIDES:${PN} += "cef"