KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
msg_bse.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2012-2015 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : msg_bse.c
11  * Created : 18 mei 2015
12  * Author : Vincent van Beveren
13  */
14 
15 #include <stdbool.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include "net/msg.h"
19 #include "kernel/err.h"
20 #include "kernel/sys.h"
21 #include "util/databuffer.h"
22 #include "errorcode.h"
23 
24 #include "subsys/sub_bse.h"
25 
26 #include "drv/uart/edfa-eau.h"
27 #include "drv/uart/bps.h"
28 
29 
30 #define MAX_UART_CMD 80
31 #define MAX_UART_RPL 80
32 
33 
34 
35 
36 bool _msgBseConfigure(MsgId * msgId, DataBuffer * cmd)
37 {
38  if (!bseConfigure()) return msgTxCurError(msgId);
39  return msgTxReplyAck(msgId);
40 }
41 
42 bool _msgBseReset(MsgId * msgId, DataBuffer * cmd)
43 {
44  if (!bseReset()) return msgTxCurError(msgId);
45  return msgTxReplyAck(msgId);
46 }
47 
48 
49 bool _msgBseEdfaDbgCmd(MsgId * msgId, DataBuffer * cmd)
50 {
51  char cmdString[MAX_UART_CMD];
52 
53 
54  dbReadString(cmd, cmdString, sizeof(cmdString));
55 
56  if (!msgRxBufCheck(cmd)) return false;
57 
58  if (!edfaDbgCmd(cmdString)) {
59  return msgTxCurError(msgId);
60  }
61 
62  // Add command to reply for single Cmd
63  uint8_t rplBuffer[MAX_UART_RPL + DB_STR_OVERHEAD];
64  DataBuffer rplDb = DB_BUF_INIT(rplBuffer, sizeof(rplBuffer));
65 
66  dbWriteString(&rplDb, cmdString, MAX_UART_RPL);
67 
68  return msgTxReply(msgId, &rplDb);
69 }
70 
71 bool _msgBseEdfaDbgCmdRply(MsgId * msgId, DataBuffer * cmd)
72 {
73  char cmdString[MAX_UART_CMD];
74  uint8_t rplBuffer[MAX_UART_RPL + DB_STR_OVERHEAD];
75  DataBuffer rplDb = DB_BUF_INIT(rplBuffer, sizeof(rplBuffer));
76 
77 
78  dbReadString(cmd, cmdString, sizeof(cmdString));
79 
80  if (!msgRxBufCheck(cmd)) return false;
81 
82  char * rpl;
83 
84  if (!edfaDbgCmdReply(cmdString, &rpl)) {
85  return msgTxCurError(msgId);
86  }
87 
88  dbWriteString(&rplDb, rpl, MAX_UART_RPL);
89 
90  return msgTxReply(msgId, &rplDb);
91 
92 }
93 
94 bool _msgBseBpsDbgCmdRply(MsgId * msgId, DataBuffer * cmd)
95 {
96  uint8_t cmdCode;
97  uint8_t cmdData[MAX_UART_CMD];
98  uint8_t cmdNData;
99  uint8_t rplCode;
100  uint8_t * rplPData;
101  uint8_t rplNData;
102 
103  int i;
104 
105 
106  dbReadU8(cmd, &cmdCode);
107  dbReadU8(cmd, &rplCode);
108  dbReadU8(cmd, &rplNData);
109  dbReadU8(cmd, &cmdNData);
110 
111 
112  if (cmdNData > MAX_UART_CMD) return errSet(ERROR(E_OUTOFMEMORY));
113 
114  for (i = 0; i < cmdNData; ++i)
115  {
116  dbReadU8(cmd, &cmdData[i]);
117  }
118 
119  if (!msgRxBufCheck(cmd)) return false;
120 
121  if (!bpsDbgCmdReply(cmdCode, cmdData, cmdNData,
122  rplCode, &rplPData, rplNData)) return msgTxCurError(msgId);
123 
124 
125  // check if your response buffer is big enough.
126  if (rplNData > MAX_UART_RPL - 3) return errSet(ERROR(E_OUTOFMEMORY));
127 
128  uint8_t rplBuf[MAX_UART_RPL];
129  DataBuffer rplDb = DB_BUF_INIT(rplBuf, sizeof(rplBuf));
130 
131  dbWriteU8(&rplDb, rplNData);
132  for (i = 0; i < rplNData; ++i)
133  {
134  dbWriteU8(&rplDb, rplPData[i]);
135  }
136 
137  // Added 1 Dec 2015: Add context to BSE.
138  dbWriteU8(&rplDb, cmdCode);
139  dbWriteU8(&rplDb, rplCode);
140 
141  return msgTxReply(msgId, &rplDb);
142 }
143 
BPS Uart driver (.
bool bpsDbgCmdReply(uint8_t cmdCode, uint8_t *cmdPData, uint8_t cmdNData, uint8_t rplCode, uint8_t **rplPData, uint8_t rplNData)
Exposes the low-level interface to the BPS.
Definition: bps.c:788
bool dbReadU8(DataBuffer *buf, uint8_t *byte)
Reads an unsigned byte.
Definition: databuffer.c:153
bool dbReadString(DataBuffer *buf, char *s, int size)
Reads a string from the stream.
Definition: databuffer.c:103
bool bseConfigure()
Base Configure event.
Definition: sub_bse.c:60
System start up and management.
Base subsystem.
bool bseReset()
Base Reset event.
Definition: sub_bse.c:66
static bool msgTxCurError(MsgId *id)
Invoke to reply the current global error.
Definition: msg.h:378
Defines a DataBuffer structure.
Definition: databuffer.h:45
Handles MCF packed messages from the higher protocol layer.
Manages the global system error.
bool msgTxReply(MsgId *id, DataBuffer *buf)
Invoke to send a reply.
Definition: msg.c:286
DataBuffer reads and writes data into a buffer in the same format as defined in the data Java DataOut...
bool msgRxBufCheck(DataBuffer *buf)
Checks the received buffer, and logs an error if there is something wrong.
Definition: msg.c:141
#define E_OUTOFMEMORY
Generic error: There is no more memory.
Definition: errorcode.h:107
This module is responsible for distributing error codes.
bool dbWriteU8(DataBuffer *buf, uint8_t byte)
Writes a unsigned byte.
Definition: databuffer.c:146
If defined, events will not require an acknowledge.
Definition: msg.h:274
bool dbWriteString(DataBuffer *buf, const char *s, int max)
Writes a String as &#39;sort of&#39; UTF-8 encoding, as defined in the Java DataOuput and DataInput writeUTF ...
Definition: databuffer.c:74
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
EDFA-EAU (30-C3-20-C) Uart driver.
#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
static bool msgTxReplyAck(MsgId *id)
Replies a simple ACK with no content.
Definition: msg.h:323
bool edfaDbgCmdReply(const char *cmd, char **retString)
Execute an EDFA debug command, and returns the reply.
Definition: edfa-eau.c:627
bool edfaDbgCmd(const char *cmd)
Execute an EDFA debug command w/o a reply.
Definition: edfa-eau.c:631