crv2_esp_now.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <Arduino.h>
  2. #include <stdio.h>
  3. #include <esp_now.h>
  4. #include <WiFi.h>
  5. #include "configuration.h"
  6. #include "crv2_esp_now.h"
  7. #include "neighbors.h"
  8. // Our broadcast peer
  9. esp_now_peer_info_t bcast;
  10. bool waiting_on_send = false;
  11. /* Send a message in an MBOX header
  12. Header:
  13. Bytes 0-3: "MBOX"
  14. Byte 4: "Message Type"
  15. Byte 5: Length, not including header.
  16. */
  17. bool send_encapsulated(const uint8_t type, const uint8_t *data, const size_t len) {
  18. static uint8_t buffer[256] = "MBOX";
  19. buffer[4] = PROTOCOL_VERSION;
  20. buffer[5] = type;
  21. buffer[6] = len;
  22. //Serial.print("send_encapsulated len = "); Serial.println(buffer[6]);
  23. memcpy(buffer+7, data, len);
  24. return send_broadcast(buffer, len + 7);
  25. }
  26. /* Decapsulate a message and route it appropriately */
  27. void process_message(receive_buffer_t& incoming) {
  28. uint8_t *buffer = incoming.data;
  29. if(buffer[0] != 77 || buffer[1] != 66 || buffer[2] != 79 || buffer[3] != 88) {
  30. Serial.print("Received non MBOX message from "); Serial.print((char *)incoming.mac_addr); Serial.println(". Ignoring.");
  31. return;
  32. }
  33. if(buffer[4] < PROTOCOL_VERSION) {
  34. Serial.print("Warning. Protocol version "); Serial.print((int) buffer[4]); Serial.print(" is earlier than expected version ("); Serial.print((int) PROTOCOL_VERSION); Serial.println("). Results may be unexpected.");
  35. }
  36. if(buffer[4] > PROTOCOL_VERSION) {
  37. Serial.print("Warning. Protocol version "); Serial.print((int) buffer[4]); Serial.print(" is greater than expected version ("); Serial.print((int) PROTOCOL_VERSION); Serial.println("). Results may be unexpected.");
  38. }
  39. uint8_t type = buffer[5];
  40. uint8_t length = buffer[6];
  41. //Serial.print("Processing message; Type = "); Serial.print(type); Serial.print("; Length = "); Serial.println(length);
  42. switch(type) {
  43. case 1:
  44. //Serial.print("Processing neighbor discovery from peer "); Serial.println((char *)incoming.mac_addr);
  45. NList.process_discovery(incoming.mac_addr, incoming.data+7, length);
  46. break;
  47. default:
  48. Serial.print("ERROR: Unsupported transmission type ");
  49. Serial.print((int) type);
  50. Serial.print(". Continuing, but results may be unexpected.");
  51. break;
  52. }
  53. }
  54. void initBroadcast() {
  55. memset(&bcast, 0, sizeof(bcast));
  56. for (int i = 0; i < 6; ++i) {
  57. bcast.peer_addr[i] = (uint8_t)0xff;
  58. }
  59. bcast.channel = CHANNEL; // pick a channel
  60. bcast.encrypt = 0; // no encryption
  61. manageBroadcast();
  62. }
  63. bool manageBroadcast() {
  64. if (bcast.channel == CHANNEL) {
  65. Serial.print("Bcast Status: ");
  66. const esp_now_peer_info_t *peer = &bcast;
  67. const uint8_t *peer_addr = bcast.peer_addr;
  68. // check if the peer exists
  69. bool exists = esp_now_is_peer_exist(peer_addr);
  70. if (exists) {
  71. // Slave already paired.
  72. Serial.println("Already Paired");
  73. return true;
  74. }
  75. else {
  76. // Slave not paired, attempt pair
  77. esp_err_t addStatus = esp_now_add_peer(peer);
  78. if (addStatus == ESP_OK) {
  79. // Pair success
  80. Serial.println("Pair success");
  81. return true;
  82. }
  83. else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) {
  84. // How did we get so far!!
  85. Serial.println("ESPNOW Not Init");
  86. return false;
  87. }
  88. else if (addStatus == ESP_ERR_ESPNOW_ARG) {
  89. Serial.println("Invalid Argument");
  90. return false;
  91. }
  92. else if (addStatus == ESP_ERR_ESPNOW_FULL) {
  93. Serial.println("Peer list full");
  94. return false;
  95. }
  96. else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) {
  97. Serial.println("Out of memory");
  98. return false;
  99. }
  100. else if (addStatus == ESP_ERR_ESPNOW_EXIST) {
  101. Serial.println("Peer Exists");
  102. return true;
  103. }
  104. else {
  105. Serial.println("Not sure what happened");
  106. return false;
  107. }
  108. }
  109. }
  110. else {
  111. // No slave found to process
  112. Serial.println("No Slave found to process");
  113. return false;
  114. }
  115. }
  116. // callback when data is received
  117. receive_buffer_t rbuf[RECEIVE_BUFFER_SIZE];
  118. int rbuf_cur=0;
  119. int rbuf_next=0;
  120. receive_buffer_t *get_message() {
  121. static receive_buffer_t msg;
  122. if(rbuf_cur == rbuf_next) {
  123. return NULL;
  124. }
  125. //if(DEBUG) {
  126. // Serial.print("Debugging get_message. Message available. cur = ");
  127. // Serial.print(rbuf_cur);
  128. // Serial.print("; next = ");
  129. // Serial.println(rbuf_next);
  130. //}
  131. memcpy(&msg, &rbuf[rbuf_cur], sizeof(receive_buffer_t));
  132. rbuf_cur++;
  133. if(rbuf_cur >= RECEIVE_BUFFER_SIZE) {
  134. rbuf_cur = 0;
  135. }
  136. return &msg;
  137. }
  138. void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  139. // Quickly copy to a buffer, then return
  140. int dest = rbuf_next;
  141. if(dest >= RECEIVE_BUFFER_SIZE) {
  142. dest = 0;
  143. if(rbuf_cur == 0) {
  144. Serial.print("WARNING: Buffer overflow. rbuf_cur="); Serial.print(rbuf_cur); Serial.print("; rbuf_next="); Serial.println(rbuf_next);
  145. return;
  146. }
  147. }
  148. if(dest == rbuf_cur - 1) {
  149. Serial.print("WARNING: Buffer overflow. rbuf_cur="); Serial.print(rbuf_cur); Serial.print("; rbuf_next="); Serial.println(rbuf_next);
  150. return;
  151. }
  152. // No buffer overflow, copy everything to our buffer
  153. snprintf((char *) rbuf[dest].mac_addr, sizeof(rbuf[dest].mac_addr), "%02x:%02x:%02x:%02x:%02x:%02x",
  154. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  155. memcpy(rbuf[dest].data, data, data_len);
  156. rbuf[dest].data[data_len] = 0x0;
  157. rbuf[dest].data_len = data_len;
  158. if(DEBUG) {
  159. //Serial.print("Data received from: "); Serial.println((char*) rbuf[dest].mac_addr);
  160. }
  161. rbuf_next = dest + 1;
  162. if(rbuf_next >= RECEIVE_BUFFER_SIZE) {
  163. rbuf_next = 0;
  164. }
  165. return;
  166. }
  167. // callback when data is sent
  168. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  169. char macStr[18];
  170. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  171. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  172. //Serial.print("Last Packet Sent to: "); Serial.println(macStr);
  173. //Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  174. waiting_on_send = false;
  175. }
  176. bool send_broadcast(const uint8_t *data, const size_t len) {
  177. assert(len > 0);
  178. assert(len < ESP_NOW_MAX_DATA_LEN);
  179. while(waiting_on_send) {
  180. // an interrupt will clear this once the last send message has been sent
  181. }
  182. //Serial.print("Sending "); Serial.print(len); Serial.println(" bytes of data.");
  183. waiting_on_send = true;
  184. esp_err_t result = esp_now_send(bcast.peer_addr, data, len);
  185. if (result == ESP_OK) {
  186. //Serial.println("Send success.");
  187. return true;
  188. }
  189. else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
  190. // How did we get so far!!
  191. Serial.println("ERROR: During send ESPNOW not Init.");
  192. }
  193. else if (result == ESP_ERR_ESPNOW_ARG) {
  194. Serial.println("ERROR: During send Invalid Argument");
  195. }
  196. else if (result == ESP_ERR_ESPNOW_INTERNAL) {
  197. Serial.println("ERROR: During send Internal Error");
  198. }
  199. else if (result == ESP_ERR_ESPNOW_NO_MEM) {
  200. Serial.println("ERROR: During send ESP_ERR_ESPNOW_NO_MEM");
  201. }
  202. else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
  203. Serial.println("ERROR: During send Peer not found.");
  204. }
  205. else {
  206. Serial.println("ERROR: During send Not sure what happened");
  207. }
  208. return false;
  209. }