-
Notifications
You must be signed in to change notification settings - Fork 0
add mqtt module #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jr1221
wants to merge
5
commits into
main
Choose a base branch
from
mqtt-ethernet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add mqtt module #164
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,23 @@ | ||
| #pragma once | ||
| #include "serverdata.pb.h" | ||
| #include "tx_api.h" | ||
| #include <stdint.h> | ||
|
|
||
| #include "nx_stm32_eth_driver.h" | ||
| #include "u_nx_ethernet.h" | ||
| #include "main.h" | ||
| #include "u_queues.h" | ||
| #define ETH_MAX_TOPIC_SIZE 100 | ||
|
|
||
| /* API */ | ||
| int ethernet1_init(void); | ||
| void ethernet_inbox(ethernet_message_t *message); | ||
| /** | ||
| * Initialize ethernet | ||
| */ | ||
| UINT ethernet1_init(void); | ||
| /** | ||
| * Send a protobuf message over MQTT | ||
| */ | ||
| UINT ethernet1_mqtt_send(char* topic, uint8_t topic_size, char* unit, uint8_t unit_size, float* values, uint8_t values_len, uint64_t time_us); | ||
|
|
||
| typedef struct { | ||
| uint8_t type; | ||
| char topic[ETH_MAX_TOPIC_SIZE]; | ||
| uint8_t topic_size; | ||
| serverdata_v2_ServerData msg; | ||
| } eth_mqtt_queue_message_t; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,107 @@ | ||
| #include "u_ethernet.h" | ||
| #include "nx_api.h" | ||
| #include "pb.h" | ||
| #include "pb_encode.h" | ||
| #include "serverdata.pb.h" | ||
| #include "tx_api.h" | ||
| #include "u_queues.h" | ||
| #include "nx_stm32_eth_driver.h" | ||
| #include "u_nx_ethernet.h" | ||
| #include "u_nx_debug.h" | ||
| #include "main.h" | ||
| #include "u_tx_debug.h" | ||
| #include "u_tx_general.h" | ||
| #include "u_tx_queues.h" | ||
|
|
||
| #define ETH_TYPE_SEND 0 | ||
| #define ETH_TYPE_RECV 1 | ||
|
|
||
| /* Callback for when a ethernet message is recieved. */ | ||
| void _ethernet_recieve(ethernet_message_t message) { | ||
| void _ethernet_recieve(eth_mqtt_queue_message_t message) { | ||
| /* Send the message to the incoming ethernet queue. */ | ||
| int status = queue_send(ð_incoming, &message, TX_NO_WAIT); | ||
| int status = queue_send(ð_manager, &message, TX_NO_WAIT); | ||
| if(status != U_SUCCESS) { | ||
| PRINTLN_ERROR("Failed to send message to the incoming ethernet queue (Status: %d).", status); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| UINT ethernet1_mqtt_send(char* topic, uint8_t topic_size, char* unit, uint8_t unit_size, float* values, uint8_t values_len, uint64_t time_us) { | ||
| serverdata_v2_ServerData msg = serverdata_v2_ServerData_init_zero; | ||
| // if (unit_size > sizeof(msg.unit)) { | ||
| // return U_ERROR; | ||
| // } | ||
| if (topic_size > ETH_MAX_TOPIC_SIZE) { | ||
| return U_ERROR; | ||
| } | ||
| // if (values_len > (sizeof(msg.values) / sizeof(float))) { | ||
| // return U_ERROR; | ||
| // } | ||
| eth_mqtt_queue_message_t sendable; | ||
| msg.time_us = time_us; | ||
| // COPIES | ||
| *(msg.unit) = *unit; | ||
| *(msg.values) = *values; | ||
| msg.values_count = values_len; | ||
| sendable.type = ETH_TYPE_SEND; | ||
| sendable.msg = msg; | ||
| memcpy(sendable.topic, topic, topic_size); | ||
| sendable.topic_size = topic_size; | ||
| return queue_send(ð_manager, &sendable, TX_NO_WAIT); | ||
| } | ||
|
|
||
| /* Initializes ethernet. */ | ||
| int ethernet1_init(void) { | ||
| UINT ethernet1_init(void) { | ||
| /* PHY_RESET Pin has to be set HIGH for the PHY to function. */ | ||
| HAL_GPIO_WritePin(PHY_RESET_GPIO_Port, PHY_RESET_Pin, GPIO_PIN_SET); | ||
|
|
||
| /* Init the ethernet. */ | ||
| return ethernet_init(VCU, nx_stm32_eth_driver, _ethernet_recieve); | ||
| } | ||
|
|
||
| /* Processes received ethernet messages. */ | ||
| void ethernet_inbox(ethernet_message_t *message) { | ||
| switch(message->message_id) { | ||
| case 5: | ||
| PRINTLN_INFO("Incoming Ethernet message: %s", message->data); | ||
| break; | ||
| case 0x02: | ||
| // do thing | ||
| break; | ||
| case 0x03: | ||
| // etc | ||
| break; | ||
| default: | ||
| uint8_t buff = 0; | ||
| buff = message->data[0]; | ||
| PRINTLN_ERROR("Unknown Ethernet Message Recieved (Message ID: %d).", message->message_id); | ||
|
|
||
| PRINTLN_INFO("sender_id=%d, recipient_id=%d, message_id=%d, data_length=%d, data=%d", message->sender_id, message->recipient_id, message->message_id, message->data_length, message->data[0]); | ||
|
|
||
| break; | ||
|
|
||
| void vEthernet1Manager(ULONG thread_input) { | ||
|
|
||
| // all calls to the ethernet driver in this thread only to prevent race conditons | ||
| UINT status = ethernet1_init(); | ||
| if(status != NX_SUCCESS) { | ||
| PRINTLN_ERROR("Failed to call ethernet1_init() (Status: %d/%s).", status, nx_status_toString(status)); | ||
| } | ||
| PRINTLN_INFO("Successfully initialized ethernet 1!"); | ||
|
|
||
| eth_mqtt_queue_message_t msg; | ||
| unsigned char buffer[serverdata_v2_ServerData_size]; | ||
| pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); | ||
|
|
||
| while(1) { | ||
|
|
||
| /* Send outgoing messages, recieve incoming messages */ | ||
| while(queue_receive(ð_manager, &msg, TX_WAIT_FOREVER) == U_SUCCESS) { | ||
| if (msg.type == ETH_TYPE_SEND) { | ||
| status = pb_encode(&stream, serverdata_v2_ServerData_fields, &msg.msg); | ||
| if (!status) { | ||
| PRINTLN_ERROR("Failed to serialize protobuf message: %s", PB_GET_ERROR(&stream)); | ||
| continue; | ||
| } | ||
| status = ethernet_mqtt_publish(msg.topic, msg.topic_size, (char*)buffer, stream.bytes_written); | ||
| stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); | ||
| if(status != U_SUCCESS) { | ||
| PRINTLN_WARNING("Failed to send Ethernet message after removing from outgoing queue: %ud", status); | ||
| if (status == 0x10002) { | ||
| do { | ||
| tx_thread_sleep(MS_TO_TICKS(1000)); | ||
| status = ethernet_mqtt_reconnect(); | ||
| PRINTLN_WARNING("ATTEMPTING MQTT RECONNECTION: Status %d", status); | ||
| } while (status != NX_SUCCESS); | ||
| PRINTLN_WARNING("MQTT RECONNECTION SUCCESS"); | ||
| } | ||
| continue; | ||
| } else { | ||
| PRINTLN_INFO("Sent ethernet message!"); | ||
| } | ||
| } | ||
jr1221 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /* No sleep. Thread timing is controlled completely by the queue timeout. */ | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nit but can we make a var for 0x10002. assuming that is mqtt disconnection return status