zykes

Untitled

May 15th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // For printf used below
  2. #include <stdio.h>
  3. #include <queue>
  4.  
  5. // PJON library
  6. #include <inttypes.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. // RPI serial interface
  10. #include <wiringPi.h>
  11. #include <wiringSerial.h>
  12.  
  13. #include "gateway.pb.h"
  14. #include "gateway.grpc.pb.h"
  15.  
  16. #include <grpc/grpc.h>
  17. #include <grpc++/channel.h>
  18. #include <grpc++/client_context.h>
  19. #include <grpc++/create_channel.h>
  20. #include <grpc++/security/credentials.h>
  21.  
  22. #ifndef RPI
  23. #define RPI true
  24. #endif
  25.  
  26. #define TS_RESPONSE_TIME_OUT 200000
  27. #define TS_COLLISION_DELAY 3000
  28.  
  29. #define PJON_INCLUDE_TS true // Include only ThroughSerial
  30. #include "PJON/PJON.h"
  31.  
  32. using std::queue;
  33.  
  34. using grpc::Channel;
  35. using grpc::ClientAsyncResponseReader;
  36. using grpc::ClientContext;
  37. using grpc::CompletionQueue;
  38. using grpc::Status;
  39.  
  40. using gatekeeper::PJONGateway;
  41. using gatekeeper::Empty;
  42. using gatekeeper::Message;
  43. using gatekeeper::SendStatus;
  44.  
  45. void receiver_function(
  46. uint8_t *payload,
  47. uint16_t length,
  48. const PJON_Packet_Info &packet_info
  49. ) {
  50. printf("%s\n", payload);
  51. }
  52.  
  53. class PJONAgent final {
  54. public:
  55. PJONAgent (PJON<ThroughSerial> *bus, std::unique_ptr<PJONGateway::Stub> *stub) {
  56. this->bus = bus;
  57. this->bus->strategy.set_tx_RS485_pin(0);
  58.  
  59. this->bus->set_receiver(receiver_function);
  60.  
  61. this->addr = (":50051");
  62. }
  63.  
  64. void run () {
  65. while (true) {
  66. this->bus->receive();
  67. this->bus->update();
  68. }
  69. }
  70.  
  71. private:
  72. std::string addr;
  73. PJON<ThroughSerial> *bus;
  74. std::unique_ptr<PJONGateway::Stub> stub_;
  75. };
  76.  
  77.  
  78. PJON<ThroughSerial> initBus() {
  79. printf("PJON instantiation... \n");
  80. PJON<ThroughSerial> bus(1);
  81.  
  82. printf("Opening serial... \n");
  83. int s = serialOpen("/dev/ttyAMA0", 9600);
  84.  
  85. if (s < 0) {
  86. printf("Serial open fail!");
  87. }
  88.  
  89. if (wiringPiSetup() == -1) {
  90. printf("WiringPi setup fail");
  91. }
  92.  
  93. printf("Setting serial... \n");
  94. bus.strategy.set_serial(s);
  95.  
  96. printf("Opening bus... \n");
  97. bus.begin();
  98.  
  99. return bus;
  100. }
  101.  
  102.  
  103. int main()
  104. {
  105. PJON<ThroughSerial> bus = initBus();
  106.  
  107. std::shared_ptr<Channel> channel = grpc::CreateChannel("localost:50051", grpc::InsecureChannelCredentials());
  108. std::unique_ptr<PJONGateway::Stub> stub = PJONGateway::NewStub(channel);
  109. PJONAgent svc(&bus, &stub);
  110.  
  111. bus.set_receiver(receiver_function);
  112.  
  113. svc.run();
  114.  
  115. printf("Attempting to send a packet... \n");
  116. bus.send(2, "B", 1);
  117.  
  118. while (true) {
  119. bus.update();
  120. }
  121.  
  122. printf("Attempting to roll bus... \n");
  123. bus.update();
  124.  
  125. printf("Attempting to receive from bus... \n");
  126. bus.receive();
  127.  
  128. printf("Success! \n");
  129. return 0;
  130. };
Add Comment
Please, Sign In to add comment