sbgECom.c
Go to the documentation of this file.
1 #include "sbgECom.h"
4 
5 //----------------------------------------------------------------------//
6 //- Private methods declarations -//
7 //----------------------------------------------------------------------//
8 
9 //----------------------------------------------------------------------//
10 //- Public methods declarations -//
11 //----------------------------------------------------------------------//
12 
20 {
21  SbgErrorCode errorCode = SBG_NO_ERROR;
22 
23  //
24  // Check input parameters
25  //
26  if ( (pInterface) && (pHandle) )
27  {
28  //
29  // Initialize the sbgECom handle
30  //
31  pHandle->pReceiveLogCallback = NULL;
32  pHandle->pUserArg = NULL;
33 
34  //
35  // Initialize the default number of trials and time out
36  //
37  pHandle->numTrials = 3;
39 
40  //
41  // Initialize the protocol
42  //
43  errorCode = sbgEComProtocolInit(&pHandle->protocolHandle, pInterface);
44  }
45  else
46  {
47  errorCode = SBG_NULL_POINTER;
48  }
49 
50  return errorCode;
51 }
52 
59 {
60  SbgErrorCode errorCode = SBG_NO_ERROR;
61 
62  //
63  // Test that we have a valid protocol handle
64  //
65  if (pHandle)
66  {
67  //
68  // Close the protocol
69  //
70  errorCode = sbgEComProtocolClose(&pHandle->protocolHandle);
71  }
72  else
73  {
74  errorCode = SBG_NULL_POINTER;
75  }
76 
77  return errorCode;
78 }
79 
86 {
87  SbgErrorCode errorCode = SBG_NO_ERROR;
88  SbgBinaryLogData logData;
89  uint8_t receivedMsg;
90  uint8_t receivedMsgClass;
91  size_t payloadSize;
92  uint8_t payloadData[SBG_ECOM_MAX_PAYLOAD_SIZE];
93 
94  //
95  // Check input arguments
96  //
97  assert(pHandle);
98 
99  //
100  // Try to read a received frame
101  //
102  errorCode = sbgEComProtocolReceive(&pHandle->protocolHandle, &receivedMsgClass, &receivedMsg, payloadData, &payloadSize, sizeof(payloadData));
103 
104  //
105  // Test if we have received a valid frame
106  //
107  if (errorCode == SBG_NO_ERROR)
108  {
109  //
110  // Test if the received frame is a binary log
111  //
112  if (sbgEComMsgClassIsALog((SbgEComClass)receivedMsgClass))
113  {
114  //
115  // The received frame is a binary log one
116  //
117  errorCode = sbgEComBinaryLogParse((SbgEComClass)receivedMsgClass, (SbgEComMsgId)receivedMsg, payloadData, payloadSize, &logData);
118 
119  //
120  // Test if the incoming log has been parsed successfully
121  //
122  if (errorCode == SBG_NO_ERROR)
123  {
124  //
125  // Test if we have a valid callback to handle received logs
126  //
127  if (pHandle->pReceiveLogCallback)
128  {
129  //
130  // Call the binary log callback using the new method
131  //
132  errorCode = pHandle->pReceiveLogCallback(pHandle, (SbgEComClass)receivedMsgClass, receivedMsg, &logData, pHandle->pUserArg);
133  }
134  }
135  else
136  {
137  //
138  // Call the on error callback
139  //
140  }
141  }
142  else
143  {
144  //
145  // We have received a command, it shouldn't happen
146  //
147  }
148  }
149  else if (errorCode != SBG_NOT_READY)
150  {
151  //
152  // We have received an invalid frame
153  //
154  SBG_LOG_WARNING(errorCode, "Invalid frame received");
155  }
156 
157  return errorCode;
158 }
159 
166 {
167  SbgErrorCode errorCode = SBG_NO_ERROR;
168 
169  //
170  // Check input arguments
171  //
172  assert(pHandle);
173 
174  //
175  // Try to read all received frames, we thus loop until we get an SBG_NOT_READY error
176  //
177  do
178  {
179  //
180  // Try to read and parse one frame
181  //
182  errorCode = sbgEComHandleOneLog(pHandle);
183  } while (errorCode != SBG_NOT_READY);
184 
185  return errorCode;
186 }
187 
195 SbgErrorCode sbgEComSetReceiveLogCallback(SbgEComHandle *pHandle, SbgEComReceiveLogFunc pReceiveLogCallback, void *pUserArg)
196 {
197  SbgErrorCode errorCode = SBG_NO_ERROR;
198 
199  //
200  // Test that we have a valid protocol handle
201  //
202  if (pHandle)
203  {
204  //
205  // Define the callback and the user argument
206  //
207  pHandle->pReceiveLogCallback = pReceiveLogCallback;
208  pHandle->pUserArg = pUserArg;
209  }
210  else
211  {
212  errorCode = SBG_NULL_POINTER;
213  }
214 
215  return errorCode;
216 }
217 
224 void sbgEComSetCmdTrialsAndTimeOut(SbgEComHandle *pHandle, uint32_t numTrials, uint32_t cmdDefaultTimeOut)
225 {
226  //
227  // Check input arguments
228  //
229  assert(pHandle);
230  assert(numTrials > 0);
231  assert(cmdDefaultTimeOut > 0);
232 
233  //
234  // Define the new settings
235  //
236  pHandle->numTrials = numTrials;
237  pHandle->cmdDefaultTimeOut = cmdDefaultTimeOut;
238 }
239 
245 void sbgEComErrorToString(SbgErrorCode errorCode, char errorMsg[256])
246 {
247  if (errorMsg)
248  {
249  //
250  // For each error code, copy the error msg
251  //
252  switch (errorCode)
253  {
254  case SBG_NO_ERROR:
255  strcpy(errorMsg, "SBG_NO_ERROR: No error.");
256  break;
257  case SBG_ERROR:
258  strcpy(errorMsg, "SBG_ERROR: Generic error.");
259  break;
260  case SBG_NULL_POINTER:
261  strcpy(errorMsg, "SBG_NULL_POINTER: A pointer is null.");
262  break;
263  case SBG_INVALID_CRC:
264  strcpy(errorMsg, "SBG_INVALID_CRC: The received frame has an invalid CRC.");
265  break;
266  case SBG_INVALID_FRAME:
267  strcpy(errorMsg, "SBG_INVALID_FRAME: The received frame is invalid.");
268  break;
269  case SBG_TIME_OUT:
270  strcpy(errorMsg, "SBG_TIME_OUT: We have a time out during frame reception.");
271  break;
272  case SBG_WRITE_ERROR:
273  strcpy(errorMsg, "SBG_WRITE_ERROR: All bytes hasn't been written.");
274  break;
275  case SBG_READ_ERROR:
276  strcpy(errorMsg, "SBG_READ_ERROR: All bytes hasn't been read.");
277  break;
278  case SBG_BUFFER_OVERFLOW:
279  strcpy(errorMsg, "SBG_BUFFER_OVERFLOW: A buffer is too small to contain so much data.");
280  break;
282  strcpy(errorMsg, "SBG_INVALID_PARAMETER: An invalid parameter has been founded.");
283  break;
284  case SBG_NOT_READY:
285  strcpy(errorMsg, "SBG_NOT_READY: A device isn't ready (Rx isn't ready for example).");
286  break;
287  case SBG_MALLOC_FAILED:
288  strcpy(errorMsg, "SBG_MALLOC_FAILED: Failed to allocate a buffer.");
289  break;
291  strcpy(errorMsg, "SGB_CALIB_MAG_NOT_ENOUGH_POINTS: Not enough points were available to perform magnetometers calibration.");
292  break;
294  strcpy(errorMsg, "SBG_CALIB_MAG_INVALID_TAKE: The calibration procedure could not be properly executed due to insufficient precision.");
295  break;
297  strcpy(errorMsg, "SBG_CALIB_MAG_SATURATION: Saturation were detected when attempt to calibrate magnetos.");
298  break;
300  strcpy(errorMsg, "SBG_CALIB_MAG_POINTS_NOT_IN_A_PLANE: 2D calibration procedure could not be performed.");
301  break;
303  strcpy(errorMsg, "SBG_DEVICE_NOT_FOUND: A device couldn't be founded or opened.");
304  break;
306  strcpy(errorMsg, "SBG_OPERATION_CANCELLED: An operation has been cancelled by a user.");
307  break;
309  strcpy(errorMsg, "SBG_NOT_CONTINUOUS_FRAME: We have received a frame that isn't a continuous one.");
310  break;
312  strcpy(errorMsg, "SBG_INCOMPATIBLE_HARDWARE: Hence valid, the configuration cannot be executed because of incompatible hardware.");
313  break;
314  default:
315  sprintf(errorMsg, "Undefined error code: %u", errorCode);
316  break;
317  }
318  }
319 }
Contains main sbgECom methods.
This file groups all common definitions required by all commands.
SbgErrorCode sbgEComProtocolReceive(SbgEComProtocol *pHandle, uint8_t *pMsgClass, uint8_t *pMsg, void *pData, size_t *pSize, size_t maxSize)
SbgErrorCode sbgEComHandleOneLog(SbgEComHandle *pHandle)
Definition: sbgECom.c:85
#define SBG_LOG_WARNING(format,...)
Definition: sbgDebug.h:75
Used to read/write data from/to a memory buffer stream.
SbgErrorCode(* SbgEComReceiveLogFunc)(SbgEComHandle *pHandle, SbgEComClass msgClass, SbgEComMsgId msg, const SbgBinaryLogData *pLogData, void *pUserArg)
Definition: sbgECom.h:61
void sbgEComSetCmdTrialsAndTimeOut(SbgEComHandle *pHandle, uint32_t numTrials, uint32_t cmdDefaultTimeOut)
Definition: sbgECom.c:224
SbgEComProtocol protocolHandle
Definition: sbgECom.h:72
SbgErrorCode sbgEComProtocolInit(SbgEComProtocol *pHandle, SbgInterface *pInterface)
uint32_t cmdDefaultTimeOut
Definition: sbgECom.h:78
SbgErrorCode sbgEComHandle(SbgEComHandle *pHandle)
Definition: sbgECom.c:165
#define NULL
Definition: sbgDefines.h:81
SbgEComReceiveLogFunc pReceiveLogCallback
Definition: sbgECom.h:74
#define SBG_ECOM_DEFAULT_CMD_TIME_OUT
SbgErrorCode sbgEComSetReceiveLogCallback(SbgEComHandle *pHandle, SbgEComReceiveLogFunc pReceiveLogCallback, void *pUserArg)
Definition: sbgECom.c:195
void * pUserArg
Definition: sbgECom.h:75
uint32_t numTrials
Definition: sbgECom.h:77
uint8_t SbgEComMsgId
Definition: sbgEComIds.h:289
enum _SbgEComClass SbgEComClass
SBG_INLINE bool sbgEComMsgClassIsALog(SbgEComClass msgClass)
Definition: sbgEComIds.h:301
void sbgEComErrorToString(SbgErrorCode errorCode, char errorMsg[256])
Definition: sbgECom.c:245
SbgErrorCode sbgEComProtocolClose(SbgEComProtocol *pHandle)
SbgErrorCode sbgEComInit(SbgEComHandle *pHandle, SbgInterface *pInterface)
Definition: sbgECom.c:19
#define SBG_ECOM_MAX_PAYLOAD_SIZE
enum _SbgErrorCode SbgErrorCode
SbgErrorCode sbgEComClose(SbgEComHandle *pHandle)
Definition: sbgECom.c:58
SbgErrorCode sbgEComBinaryLogParse(SbgEComClass msgClass, SbgEComMsgId msg, const void *pPayload, size_t payloadSize, SbgBinaryLogData *pOutputData)


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