Rev 4 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4 | Rev 5 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* USER CODE BEGIN Header */ |
1 | /* USER CODE BEGIN Header */ |
- | 2 | ||
2 | /** |
3 | /** |
3 | ****************************************************************************** |
4 | ****************************************************************************** |
4 | * @file : main.c |
5 | * @file : main.c |
5 | * @brief : Main program body |
6 | * @brief : Main program body |
6 | ****************************************************************************** |
7 | ****************************************************************************** |
Line 14... | Line 15... | ||
14 | * License. You may obtain a copy of the License at: |
15 | * License. You may obtain a copy of the License at: |
15 | * opensource.org/licenses/BSD-3-Clause |
16 | * opensource.org/licenses/BSD-3-Clause |
16 | * |
17 | * |
17 | ****************************************************************************** |
18 | ****************************************************************************** |
18 | */ |
19 | */ |
- | 20 | /** @note |
|
- | 21 | * Reference : implementation created from reversing code in: |
|
- | 22 | * https://github.com/canboat/canboat/tree/master/ikonvert-serial |
|
- | 23 | */ |
|
- | 24 | ||
19 | /* USER CODE END Header */ |
25 | /* USER CODE END Header */ |
20 | /* Includes ------------------------------------------------------------------*/ |
26 | /* Includes ------------------------------------------------------------------*/ |
21 | #include "main.h" |
27 | #include "main.h" |
22 | 28 | ||
23 | /* Private includes ----------------------------------------------------------*/ |
29 | /* Private includes ----------------------------------------------------------*/ |
24 | /* USER CODE BEGIN Includes */ |
30 | /* USER CODE BEGIN Includes */ |
- | 31 | #include <stdio.h> |
|
- | 32 | #include <stdlib.h> |
|
- | 33 | ||
25 | #include "display.h" |
34 | #include "display.h" |
26 | 35 | ||
27 | #include "string.h" |
36 | #include "string.h" |
28 | #include "base64.h" |
37 | #include "base64.h" |
29 | #include "libSerial/serial.H" |
38 | #include "libSerial/serial.H" |
Line 104... | Line 113... | ||
104 | 113 | ||
105 | uint32_t TxMailbox; |
114 | uint32_t TxMailbox; |
106 | 115 | ||
107 | char const version[] = "$PDGY,Mike_James_Converter_#00001_Mode:15\r\n"; |
116 | char const version[] = "$PDGY,Mike_James_Converter_#00001_Mode:15\r\n"; |
108 | char const keepawake[] = "$PDGY,000000,1,,4,%ld.%03ld,,,\r\n"; |
117 | char const keepawake[] = "$PDGY,000000,1,,4,%ld.%03ld,,,\r\n"; |
- | 118 | char const commandHdr[] = "$PDGY,"; |
|
109 | 119 | ||
110 | // Allocation of serial buffer sizes and storage |
120 | // Allocation of serial buffer sizes and storage |
111 | #define TX_BUFFER_SIZE 512 |
121 | #define TX_BUFFER_SIZE 512 |
112 | #define RX_BUFFER_SIZE 16 |
122 | #define RX_BUFFER_SIZE 16 |
113 | uint8_t tx_buffer[TX_BUFFER_SIZE]; |
123 | uint8_t tx_buffer[TX_BUFFER_SIZE]; |
Line 270... | Line 280... | ||
270 | } |
280 | } |
271 | 281 | ||
272 | void processCmd(char *buff, int len) |
282 | void processCmd(char *buff, int len) |
273 | { |
283 | { |
274 | buff[len] = 0; // terminate in case of error |
284 | buff[len] = 0; // terminate in case of error |
- | 285 | /// expect $PDGY,pgn,dst,payloadBase64 |
|
275 | char lineBuff[100]; |
286 | const char tokens[] = ","; |
- | 287 | ||
- | 288 | char *s = strtok(buff, tokens); |
|
- | 289 | if (strcmp(s, "$PDGY") != 0) |
|
- | 290 | return; |
|
- | 291 | ||
- | 292 | char *pgnStr = strtok(NULL, tokens); |
|
- | 293 | if (!pgnStr) |
|
- | 294 | return; |
|
- | 295 | char *dstStr = strtok(NULL, tokens); |
|
- | 296 | if (!dstStr) |
|
- | 297 | return; |
|
- | 298 | char *payloadStr = strtok(NULL, tokens); |
|
- | 299 | if (!payloadStr) |
|
- | 300 | return; |
|
- | 301 | ||
276 | int pos = small_sprintf(lineBuff, "$PDGY,ACK,%s\r\n", buff); |
302 | // decode string into lineBuff from base64 |
- | 303 | uint8_t frameData[8]; |
|
- | 304 | size_t bytes = 0; |
|
- | 305 | base64_decode(payloadStr, strlen(payloadStr), (char *) frameData, &bytes); |
|
- | 306 | ||
277 | if (pos < SerialTransmitSpace(&uc1)) |
307 | int pgn = atoi(pgnStr); |
- | 308 | ||
- | 309 | // int dst = atoi(dstStr); |
|
- | 310 | ||
- | 311 | CAN_TxHeaderTypeDef Header; |
|
- | 312 | Header.ExtId = pgn; |
|
- | 313 | Header.DLC = bytes < 8 ? bytes : 8; |
|
- | 314 | Header.IDE = 0; |
|
- | 315 | Header.RTR = 0; |
|
- | 316 | ||
- | 317 | uint32_t usedMailbox; |
|
- | 318 | HAL_StatusTypeDef status = HAL_CAN_AddTxMessage(&hcan, &Header, |
|
- | 319 | (uint8_t *)frameData, &usedMailbox); |
|
- | 320 | ||
- | 321 | if (status == HAL_OK) |
|
278 | { |
322 | { |
- | 323 | char lineBuff[100]; |
|
- | 324 | int pos = small_sprintf(lineBuff, "$PDGY,ACK,%s\r\n", buff); |
|
- | 325 | if (pos < SerialTransmitSpace(&uc1)) |
|
- | 326 | { |
|
279 | __disable_irq(); |
327 | __disable_irq(); |
280 | sendString(&uc1, (char *)lineBuff, pos); |
328 | sendString(&uc1, (char *)lineBuff, pos); |
281 | __enable_irq(); |
329 | __enable_irq(); |
- | 330 | } |
|
282 | } |
331 | } |
283 | } |
332 | } |
284 | 333 | ||
285 | /* USER CODE END 0 */ |
334 | /* USER CODE END 0 */ |
286 | 335 |