sbgEComCmdInterface.c
Go to the documentation of this file.
1 #include "sbgEComCmdInterface.h"
3 
4 //----------------------------------------------------------------------//
5 //- Interface commands -//
6 //----------------------------------------------------------------------//
7 
9 {
10  SbgErrorCode errorCode = SBG_NO_ERROR;
11  uint32_t trial;
12  size_t receivedSize;
13  uint8_t receivedBuffer[SBG_ECOM_MAX_BUFFER_SIZE];
14  SbgStreamBuffer inputStream;
15  uint8_t outputBuffer;
16 
17  assert(pHandle);
18  assert(pConf);
19 
20  //
21  // Send the command three times
22  //
23  for (trial = 0; trial < pHandle->numTrials; trial++)
24  {
25  //
26  // Send the command and the interfaceId as a 1-byte payload
27  //
28  outputBuffer = (uint8_t)interfaceId;
29  errorCode = sbgEComProtocolSend(&pHandle->protocolHandle, SBG_ECOM_CLASS_LOG_CMD_0, SBG_ECOM_CMD_UART_CONF, &outputBuffer, sizeof(uint8_t));
30 
31  //
32  // Make sure that the command has been sent
33  //
34  if (errorCode == SBG_NO_ERROR)
35  {
36  //
37  // Try to read the device answer for 500 ms
38  //
39  errorCode = sbgEComReceiveCmd(pHandle, SBG_ECOM_CLASS_LOG_CMD_0, SBG_ECOM_CMD_UART_CONF, receivedBuffer, &receivedSize, sizeof(receivedBuffer), pHandle->cmdDefaultTimeOut);
40 
41  //
42  // Test if we have received correctly the answer
43  //
44  if (errorCode == SBG_NO_ERROR)
45  {
46  //
47  // Initialize stream buffer to read parameters
48  //
49  sbgStreamBufferInitForRead(&inputStream, receivedBuffer, receivedSize);
50 
51  //
52  // Read parameters
53  // First is returned interfaceId, then baud rate and the mode at last.
54  //
55  interfaceId = (SbgEComPortId)sbgStreamBufferReadUint8LE(&inputStream);
56  pConf->baudRate = sbgStreamBufferReadUint32LE(&inputStream);
57  pConf->mode = (SbgEComPortMode)sbgStreamBufferReadUint8LE(&inputStream);
58 
59  //
60  // The command has been executed successfully so return
61  //
62  break;
63  }
64  }
65  else
66  {
67  //
68  // We have a write error so exit the try loop
69  //
70  break;
71  }
72  }
73 
74  return errorCode;
75 }
76 
78 {
79  SbgErrorCode errorCode = SBG_NO_ERROR;
80  uint32_t trial;
81  uint8_t outputBuffer[SBG_ECOM_MAX_BUFFER_SIZE];
82  SbgStreamBuffer outputStream;
83 
84  assert(pHandle);
85  assert(pConf);
86 
87  //
88  // Send the command three times
89  //
90  for (trial = 0; trial < pHandle->numTrials; trial++)
91  {
92  //
93  // Init stream buffer for output
94  //
95  sbgStreamBufferInitForWrite(&outputStream, outputBuffer, sizeof(outputBuffer));
96 
97  //
98  // Build payload
99  //
100  sbgStreamBufferWriteUint8LE(&outputStream, (uint8_t)interfaceId);
101  sbgStreamBufferWriteUint32LE(&outputStream, pConf->baudRate);
102  sbgStreamBufferWriteUint8LE(&outputStream, (uint8_t)pConf->mode);
103 
104  //
105  // Send the payload over ECom
106  //
108 
109  //
110  // Make sure that the command has been sent
111  //
112  if (errorCode == SBG_NO_ERROR)
113  {
114  //
115  // Try to read the device answer for 500 ms
116  //
118 
119  //
120  // Test if we have received a valid ACK
121  //
122  if (errorCode == SBG_NO_ERROR)
123  {
124  //
125  // The command has been executed successfully so return
126  //
127  break;
128  }
129  }
130  else
131  {
132  //
133  // We have a write error so exit the try loop
134  //
135  break;
136  }
137  }
138 
139  return errorCode;
140 }
141 
143 {
144  SbgErrorCode errorCode = SBG_NO_ERROR;
145  uint32_t trial;
146  size_t receivedSize;
147  uint8_t receivedBuffer[SBG_ECOM_MAX_BUFFER_SIZE];
148  SbgStreamBuffer inputStream;
149 
150  assert(pHandle);
151  assert(pBitrate);
152  assert(pMode);
153 
154  //
155  // Send the command three times
156  //
157  for (trial = 0; trial < pHandle->numTrials; trial++)
158  {
159  //
160  // Send the command with no payload
161  //
163 
164  //
165  // Make sure that the command has been sent
166  //
167  if (errorCode == SBG_NO_ERROR)
168  {
169  //
170  // Try to read the device answer for 500 ms
171  //
172  errorCode = sbgEComReceiveCmd(pHandle, SBG_ECOM_CLASS_LOG_CMD_0, SBG_ECOM_CMD_CAN_BUS_CONF, receivedBuffer, &receivedSize, sizeof(receivedBuffer), pHandle->cmdDefaultTimeOut);
173 
174  //
175  // Test if we have received a SBG_ECOM_CMD_CAN_BUS_CONF command
176  //
177  if (errorCode == SBG_NO_ERROR)
178  {
179  //
180  // Initialize stream buffer to read parameters
181  //
182  sbgStreamBufferInitForRead(&inputStream, receivedBuffer, receivedSize);
183 
184  //
185  // Read bit rate returned by the device
186  //
187  *pBitrate = (SbgEComCanBitRate)sbgStreamBufferReadUint16LE(&inputStream);
188 
189  //
190  // Check if we can parse the CAN mode that has been introduced in sbgECom version 2.0
191  //
192  if (sbgStreamBufferGetSpace(&inputStream) > 0)
193  {
194  //
195  // Read mode returned by the device
196  //
197  *pMode = (SbgEComCanMode)sbgStreamBufferReadUint8(&inputStream);
198  }
199  else
200  {
201  //
202  // Default the mode to the behavior prior to CAN mode setting introduction
203  //
204  *pMode = SBG_ECOM_CAN_MODE_NORMAL;
205  }
206 
207  //
208  // The command has been executed successfully so return
209  //
210  break;
211  }
212  }
213  else
214  {
215  //
216  // We have a write error so exit the try loop
217  //
218  break;
219  }
220  }
221 
222  return errorCode;
223 }
224 
226 {
227  SbgErrorCode errorCode = SBG_NO_ERROR;
228  uint32_t trial;
229  uint8_t outputBuffer[3];
230  SbgStreamBuffer outputStream;
231 
232  assert(pHandle);
233  assert(bitrate <= UINT16_MAX);
234  assert(mode <= UINT8_MAX);
235 
236  //
237  // Build the command payload
238  //
239  sbgStreamBufferInitForWrite(&outputStream, outputBuffer, sizeof(outputBuffer));
240 
241  sbgStreamBufferWriteUint16LE(&outputStream, (uint16_t)bitrate);
242  sbgStreamBufferWriteUint8(&outputStream, (uint8_t)mode);
243 
244  //
245  // Send the command three times
246  //
247  for (trial = 0; trial < pHandle->numTrials; trial++)
248  {
249  //
250  // Send the payload over ECom
251  //
253 
254  //
255  // Make sure that the command has been sent
256  //
257  if (errorCode == SBG_NO_ERROR)
258  {
259  //
260  // Try to read the device answer for 500 ms
261  //
263 
264  //
265  // Test if we have received a valid ACK
266  //
267  if (errorCode == SBG_NO_ERROR)
268  {
269  //
270  // The command has been executed successfully so return
271  //
272  break;
273  }
274  }
275  else
276  {
277  //
278  // We have a write error so exit the try loop
279  //
280  break;
281  }
282  }
283 
284  return errorCode;
285 }
SBG_INLINE SbgErrorCode sbgStreamBufferInitForRead(SbgStreamBuffer *pHandle, const void *pLinkedBuffer, size_t bufferSize)
SbgErrorCode sbgEComCmdInterfaceGetCanConf(SbgEComHandle *pHandle, SbgEComCanBitRate *pBitrate, SbgEComCanMode *pMode)
enum _SbgEComPortId SbgEComPortId
SBG_INLINE SbgErrorCode sbgStreamBufferWriteUint8(SbgStreamBuffer *pHandle, uint8_t value)
SBG_INLINE uint8_t sbgStreamBufferReadUint8(SbgStreamBuffer *pHandle)
Used to read/write data from/to a memory buffer stream.
SbgErrorCode sbgEComProtocolSend(SbgEComProtocol *pHandle, uint8_t msgClass, uint8_t msg, const void *pData, size_t size)
enum _SbgEComCanBitRate SbgEComCanBitRate
SbgEComProtocol protocolHandle
Definition: sbgECom.h:72
SBG_INLINE SbgErrorCode sbgStreamBufferInitForWrite(SbgStreamBuffer *pHandle, void *pLinkedBuffer, size_t bufferSize)
SBG_INLINE uint16_t sbgStreamBufferReadUint16LE(SbgStreamBuffer *pHandle)
SbgErrorCode sbgEComWaitForAck(SbgEComHandle *pHandle, uint8_t msgClass, uint8_t msg, uint32_t timeOut)
uint32_t cmdDefaultTimeOut
Definition: sbgECom.h:78
SBG_INLINE size_t sbgStreamBufferGetSpace(SbgStreamBuffer *pHandle)
enum _SbgEComCanMode SbgEComCanMode
#define sbgStreamBufferWriteUint8LE
SBG_INLINE void * sbgStreamBufferGetLinkedBuffer(SbgStreamBuffer *pHandle)
#define NULL
Definition: sbgDefines.h:81
SbgErrorCode sbgEComCmdInterfaceSetUartConf(SbgEComHandle *pHandle, SbgEComPortId interfaceId, const SbgEComInterfaceConf *pConf)
SbgErrorCode sbgEComCmdInterfaceGetUartConf(SbgEComHandle *pHandle, SbgEComPortId interfaceId, SbgEComInterfaceConf *pConf)
SbgErrorCode sbgEComReceiveCmd(SbgEComHandle *pHandle, uint8_t msgClass, uint8_t msg, void *pData, size_t *pSize, size_t maxSize, uint32_t timeOut)
#define sbgStreamBufferReadUint8LE
uint32_t numTrials
Definition: sbgECom.h:77
SbgErrorCode sbgEComCmdInterfaceSetCanConf(SbgEComHandle *pHandle, SbgEComCanBitRate bitrate, SbgEComCanMode mode)
enum _SbgEComPortMode SbgEComPortMode
This file implements SbgECom commands related to interfaces.
SBG_INLINE SbgErrorCode sbgStreamBufferWriteUint32LE(SbgStreamBuffer *pHandle, uint32_t value)
SBG_INLINE size_t sbgStreamBufferGetLength(SbgStreamBuffer *pHandle)
SBG_INLINE uint32_t sbgStreamBufferReadUint32LE(SbgStreamBuffer *pHandle)
SBG_INLINE SbgErrorCode sbgStreamBufferWriteUint16LE(SbgStreamBuffer *pHandle, uint16_t value)
enum _SbgErrorCode SbgErrorCode
#define SBG_ECOM_MAX_BUFFER_SIZE


sbg_driver
Author(s): SBG Systems
autogenerated on Sat Sep 3 2022 02:53:36