diff options
| author | Aditya Naik | 2020-03-24 13:53:44 -0400 |
|---|---|---|
| committer | Aditya Naik | 2020-03-24 13:53:44 -0400 |
| commit | 553d9c7d470dcf2dadbfefb409cb13314c79fd9d (patch) | |
| tree | a179223f6b4afe45427d612529e580de768eb118 /src | |
| parent | 73c032002d4fcfb52980a841f3bbe6cdb868806e (diff) | |
Initial working MDR request, now ACK
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 86 | ||||
| -rw-r--r-- | src/main.h | 2 |
2 files changed, 71 insertions, 17 deletions
@@ -23,7 +23,12 @@ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
-
+#include <stdio.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "handshake.pb.h"
+#include "devices.h"
+#include "config.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -37,7 +42,13 @@ /* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
+#define device_MDR s2m_MDR_response
+#define GET_IDX_FROM_ADDR(i2c_addr) i2c_addr-1
+#define GET_BIT_FROM_IDX(a, b) a[b>>5]&(1<<(b%32))
+#define SET_BIT_FROM_IDX(a, b) a[b>>5]|=(1<<(b%32))
+#define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))
+#define MASTER
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
@@ -55,7 +66,7 @@ static void MX_GPIO_Init(void); static void MX_I2C1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
-
+bool encode_subscription_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
@@ -102,8 +113,39 @@ int main(void) /* USER CODE BEGIN WHILE */
while (1)
{
- /* USER CODE END WHILE */
-
+#ifdef MASTER
+
+#else
+ uint8_t buffer[128], debug_buf[128];
+ s2m_MDR_response res;
+ res.MDR_version=1.1;
+ res.module_id = 1;
+ res.module_class=1;
+ res.entity_id=32;
+
+ res.subscriptions.funcs.encode=encode_subscription_callback;
+ pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+ if(!pb_encode(&ostream, s2m_MDR_response_fields, &res)){
+ HAL_UART_Transmit(&huart1, "MDR encoding error\r\n", 20, 100);
+ while(1) {
+ HAL_GPIO_TogglePin(led_Pin, led_GPIO_Port);
+ }
+ }
+ else {
+ size_t enc_size = ostream.bytes_written;
+ sprintf(debug_buf, "Encoded size: %ld\r\n", enc_size);
+ HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
+ memset(debug_buf, 0, 128);
+
+ HAL_UART_Transmit(&huart1, "Buffer: ", 8, 100);
+ for(int x=0; x<enc_size; x++) {
+ sprintf(debug_buf+x, "%x", buffer[x]);
+ }
+ HAL_UART_Transmit(&huart1, debug_buf, 1, 100);
+
+ }
+#endif
+ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
@@ -232,9 +274,6 @@ static void MX_GPIO_Init(void) /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(ledA4_GPIO_Port, ledA4_Pin, GPIO_PIN_RESET);
-
/*Configure GPIO pin : led_Pin */
GPIO_InitStruct.Pin = led_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
@@ -242,17 +281,34 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(led_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : ledA4_Pin */
- GPIO_InitStruct.Pin = ledA4_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(ledA4_GPIO_Port, &GPIO_InitStruct);
-
}
/* USER CODE BEGIN 4 */
-
+bool encode_subscription_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg)
+{
+ if(ostream!=NULL && field->tag == s2m_MDR_response_subscriptions_tag) {
+ for (int x=0; x<5; x++) {
+ _subscriptions subs;
+ subs.module_id = x+10*x;
+ subs.i2c_address = x+1;
+ subs.has_entity_id=false;
+ subs.has_module_class=false;
+ subs.has_i2c_address=true;
+ if(!pb_encode_tag_for_field(ostream, field)){
+ printf("ERR1\n");
+ return false;
+ }
+ if(!pb_encode_submessage(ostream, _subscriptions_fields, &subs)){
+ printf("ERR2\n");
+ return false;
+ }
+ }
+ }
+ else{
+ return false;
+ }
+ return true;
+}
/* USER CODE END 4 */
/**
@@ -60,8 +60,6 @@ void Error_Handler(void); /* Private defines -----------------------------------------------------------*/
#define led_Pin GPIO_PIN_13
#define led_GPIO_Port GPIOC
-#define ledA4_Pin GPIO_PIN_4
-#define ledA4_GPIO_Port GPIOA
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
|
