KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
contun.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2012-2015 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : contun.c
11  * Created : 6 okt. 2015
12  * Author : Vincent van Beveren
13  */
14 
15 #include "kernel/contun.h"
16 #include "kernel/scheduler.h"
17 
18 #include "lm32soc/dev_soc.h"
19 #include "util/log.h"
20 #include "errorcode.h"
21 
22 LOG_DEF(ConsoleTunnel)
23 
24 static SockAddr _addr;
25 static bool _init = false;
26 static int _conTunTaskId = TASK_ID_NONE;
27 
28 #define _STRBUF_MAX 240
29 #define _MSGBUF_MAX 512
30 
31 // drains the simple UART virtual console buffer and puts it into a string.
32 static void rxVUart(SUART_Device * suart, char * tgtStr, size_t tgtSize)
33 {
34  char * lastChar = tgtStr + tgtSize - 1;
35  uint32_t hostRx = suart->HOST_RX;
36  while (hostRx & SUART_HOST_RX_READY)
37  {
38  *tgtStr = hostRx & SUART_HOST_RX_DATAMASK;
39  tgtStr++;
40  if (tgtStr == lastChar) break;
41  hostRx = suart->HOST_RX;
42  }
43  *tgtStr = '\0';
44 }
45 
46 // sends data. Since this function is blocking, treat with care.
47 static void txVUart(SUART_Device * suart, char * srcStr)
48 {
49  while (*srcStr != '\0')
50  {
51  if (*srcStr != '\n') {
52  // wait for RX to be not ready so we know the RX side is ready to receive
53  while( suart->STATUS & SUART_STATUS_RX_RDY) {};
54  // write char
55  suart->HOST_TX = SUART_HOST_TX_DATAMASK & *srcStr;
56  }
57  srcStr++;
58  }
59 }
60 
61 bool ignore = false;
62 
63 static void conTunTask()
64 {
65  char strBuf[_STRBUF_MAX];
66  uint8_t msgBuf[_MSGBUF_MAX];
67  DataBuffer db = DB_BUF_INIT(msgBuf, sizeof(msgBuf));
68 
69  rxVUart(PTP_SUART, strBuf, sizeof(strBuf));
70  dbWriteString(&db, strBuf, sizeof(strBuf));
71  rxVUart(SUART1, strBuf, sizeof(strBuf));
72  dbWriteString(&db, strBuf, sizeof(strBuf));
73 
74  // any data to send? (two empty strings = 4 bytes)
75  if (dbLength(&db) > DB_STR_OVERHEAD * 2)
76  {
78  }
79 }
80 
81 bool conTunInit(SockAddr * addr)
82 {
83  // if addr == NULL, tunnel should be deinitialized.
84  if (addr == NULL)
85  {
86  _init = false;
87  if (_conTunTaskId != TASK_ID_NONE) schdStop(_conTunTaskId);
88  logInfo("Console tunnel closed");
89  return true;
90  }
91 
92  if (_init) return errSet(ERROR_CTX(E_INVSTATE));
93 
94  _addr = *addr;
95 
96  // if task ID is none, register a new scheduler.
97  if (_conTunTaskId == TASK_ID_NONE)
98  {
99  if (!schdRegister(conTunTask, false, &_conTunTaskId)) return false;
100  }
101 
102  // configure to run at 50Hz. Should be enough.
103  schdRunPeriodic(_conTunTaskId, 20);
104  _init = true;
105  logInfo("Console tunnel opened");
106  return true;
107 }
108 
109 void conTunSend(char * ptp, char * secLm32)
110 {
111  if (!_init) return;
112 
113  if (ptp != NULL && strlen(ptp) > 0) {
114  txVUart(PTP_SUART, ptp);
115  }
116  if (secLm32 != NULL && strlen(secLm32) > 0) {
117  txVUart(SUART1, secLm32);
118  }
119 }
120 
121 /*
122 void conTunRecv(char * ptp, char * secLm32, int max)
123 {
124  if (!_init) return;
125 
126  if (ptp != NULL) {
127  rxVUart(PTP_SUART, ptp, max);
128  }
129  if (secLm32 != NULL) {
130  rxVUart(SUART1, secLm32, max);
131  }
132 
133 }
134 */
135 
136 
void schdRunPeriodic(int taskId, int interval)
Schedule a task to run periodically.
Definition: scheduler.c:212
const volatile unsigned int STATUS
Status Register.
Definition: dev_suart.h:41
bool msgTxEvent(int msgType, DataBuffer *buf)
Invoked to send an event.
Definition: msg.c:269
bool schdRegister(SchdTaskF task, bool priority, int *taskId)
Register a task with the scheduler.
Definition: scheduler.c:95
Simple task scheduler for tasks.
#define SUART_STATUS_RX_RDY
Uart has rx data ready.
Definition: dev_suart.h:29
const volatile unsigned int HOST_RX
Host receive register.
Definition: dev_suart.h:46
Structure defines White Rabbit Simple Uart.
Definition: dev_suart.h:39
void schdStop(int taskId)
Stop a scheduled task.
Definition: scheduler.c:223
#define E_INVSTATE
Generic error: Module is in a state in which.
Definition: errorcode.h:103
#define SUART1
SUART base pointer.
Definition: dev_soc.h:57
Defines a DataBuffer structure.
Definition: databuffer.h:45
#define PTP_SUART
PTP UART in WR space.
Definition: dev_soc.h:79
static uint32_t dbLength(DataBuffer *buf)
Returns the lenght of the current buffer.
Definition: databuffer.h:83
#define EVT_SYS_CONTUN_RECV
Event when character data is available on any of the consoles.
Definition: cfg_msg.h:233
void conTunSend(char *ptp, char *secLm32)
Send characters to PTP core and second LM32.
Definition: contun.c:109
Combination of IP address and port.
Definition: net.h:31
This modules provides console tunneling, for both PTP core and 2nd LM32.
This module is responsible for distributing error codes.
#define LOG_DEF(NAME,...)
Define a logger for a module.
Definition: log.h:129
bool conTunInit(SockAddr *addr)
Initializes the console tunnel.
Definition: contun.c:81
bool dbWriteString(DataBuffer *buf, const char *s, int max)
Writes a String as 'sort of' UTF-8 encoding, as defined in the Java DataOuput and DataInput writeUTF ...
Definition: databuffer.c:74
This file assigns all device structures to memory mapped structures.
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
Implements a generic logger facility.
#define DB_STR_OVERHEAD
Overhead for a String.
Definition: databuffer.h:40
#define DB_BUF_INIT(PTR, LEN)
Simple buffer initialization.
Definition: databuffer.h:63
volatile unsigned int HOST_TX
Host Transmit register.
Definition: dev_suart.h:45
#define logInfo(MSG,...)
Write a log message with formatting on info level.
Definition: log.h:202