aboutsummaryrefslogtreecommitdiff
path: root/projects/hsm/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'projects/hsm/main.c')
-rw-r--r--projects/hsm/main.c100
1 files changed, 33 insertions, 67 deletions
diff --git a/projects/hsm/main.c b/projects/hsm/main.c
index 2508f07..79c567b 100644
--- a/projects/hsm/main.c
+++ b/projects/hsm/main.c
@@ -45,6 +45,8 @@
* for the main thread to create the dispatch thread.
*/
+#include <string.h>
+
#include "cmsis_os.h"
#include "stm-init.h"
@@ -56,8 +58,9 @@
#define HAL_OK HAL_OKAY
#include "hal.h"
-#include "hal_internal.h" /* hal_rpc_sendto, hal_rpc_recvfrom */
-#include "xdr_internal.h" /* hal_xdr_encode_int */
+#include "hal_internal.h"
+#include "slip_internal.h"
+#include "xdr_internal.h"
/* RPC buffers. For each active RPC, there will be two - input and output.
*/
@@ -97,22 +100,6 @@ static rpc_buffer_t *rpc_buffer_alloc(void)
osMutexId uart_mutex;
osMutexDef(uart_mutex);
-/* Borrowed from xdr.c. We know the target architecture is little-endian,
- * but we pretend for the sake of appearances.
- */
-#ifdef __ARMEL__ /* little endian */
-static inline uint32_t htonl(uint32_t w)
-{
- return
- ((w & 0x000000ff) << 24) +
- ((w & 0x0000ff00) << 8) +
- ((w & 0x00ff0000) >> 8) +
- ((w & 0xff000000) >> 24);
-}
-#else
-#define htonl(x) (x)
-#endif
-
/* Thread entry point for the RPC request handler.
*/
static void dispatch_thread(void const *args)
@@ -120,18 +107,24 @@ static void dispatch_thread(void const *args)
rpc_buffer_t *ibuf = (rpc_buffer_t *)args;
rpc_buffer_t *obuf = rpc_buffer_alloc();
if (obuf == NULL) {
- uint32_t err = htonl(HAL_ERROR_ALLOCATION_FAILURE);
+ uint8_t buf[8];
+ uint8_t * bufptr = &buf[4];
+ const uint8_t * const limit = buf + sizeof(buf);
+ memcpy(buf, ibuf->buf, 4);
+ hal_xdr_encode_int(&bufptr, limit, HAL_ERROR_ALLOCATION_FAILURE);
osMutexWait(uart_mutex, osWaitForever);
- hal_rpc_sendto((uint8_t *)&err, 4, NULL);
+ hal_rpc_sendto(ibuf->buf, sizeof(buf), NULL);
osMutexRelease(uart_mutex);
osPoolFree(rpc_buffer_pool, ibuf);
Error_Handler();
}
- obuf->len = sizeof(obuf->buf);
- hal_rpc_server_dispatch(ibuf->buf, ibuf->len, obuf->buf, &obuf->len);
+ /* copy client ID from request to response */
+ memcpy(obuf->buf, ibuf->buf, 4);
+ obuf->len = sizeof(obuf->buf) - 4;
+ hal_rpc_server_dispatch(ibuf->buf + 4, ibuf->len - 4, obuf->buf + 4, &obuf->len);
osPoolFree(rpc_buffer_pool, ibuf);
osMutexWait(uart_mutex, osWaitForever);
- hal_error_t ret = hal_rpc_sendto(obuf->buf, obuf->len, NULL);
+ hal_error_t ret = hal_rpc_sendto(obuf->buf, obuf->len + 4, NULL);
osMutexRelease(uart_mutex);
osPoolFree(rpc_buffer_pool, obuf);
if (ret != HAL_OK)
@@ -147,55 +140,28 @@ osSemaphoreDef(rpc_sem);
static uint8_t c; /* current character received from UART */
static rpc_buffer_t *ibuf; /* current RPC input buffer */
-/* Add a byte to the input buffer.
- */
-static inline void ibuf_push(uint8_t c) {
- if (ibuf->len < MAX_PKT_SIZE)
- ibuf->buf[ibuf->len++] = c;
-}
-
/* Callback for HAL_UART_Receive_IT().
*/
-void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
+void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart)
{
-/* SLIP special characters */
-#define END 0300 /* indicates end of packet */
-#define ESC 0333 /* indicates byte stuffing */
-#define ESC_END 0334 /* ESC ESC_END means END data byte */
-#define ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
+ int complete;
+ hal_slip_recv_char(ibuf->buf, &ibuf->len, sizeof(ibuf->buf), &complete);
+ if (complete)
+ osSemaphoreRelease(rpc_sem);
- static int esc_flag = 0; /* previous char was ESC */
+ HAL_UART_Receive_IT(huart, &c, 1);
+}
- /* got 1 char - un-SLIP it */
- switch (c) {
- case END:
- if (ibuf->len)
- osSemaphoreRelease(rpc_sem);
- break;
- case ESC:
- esc_flag = 1;
- break;
- default:
- if (esc_flag) {
- esc_flag = 0;
- switch (c) {
- case ESC_END:
- ibuf_push(END);
- break;
- case ESC_ESC:
- ibuf_push(ESC);
- break;
- default:
- ibuf_push(c);
- }
- }
- else {
- ibuf_push(c);
- }
- break;
- }
+hal_error_t hal_serial_send_char(uint8_t c)
+{
+ return (uart_send_char(c) == 0) ? HAL_OK : HAL_ERROR_RPC_TRANSPORT;
+}
- HAL_UART_Receive_IT(huart, &c, 1);
+hal_error_t hal_serial_recv_char(uint8_t *cp)
+{
+ /* return the character from HAL_UART_Receive_IT */
+ *cp = c;
+ return HAL_OK;
}
/* The main thread. After the system setup, it waits for the RPC-request
@@ -213,8 +179,8 @@ int main()
led_toggle(LED_BLUE);
}
led_off(LED_BLUE);
- led_on(LED_GREEN);
#endif
+ led_on(LED_GREEN);
/* Prepare FMC interface. */
fmc_init();