Dummy Lora Packet Sending

Fork of Dealer_18feb17 by kumar singh

Revision:
12:6107b32b0729
Parent:
11:77e595130230
Child:
13:8955f2e95021
diff -r 77e595130230 -r 6107b32b0729 main.cpp
--- a/main.cpp	Fri Jan 27 18:30:02 2017 +0000
+++ b/main.cpp	Mon Jan 30 18:31:30 2017 +0000
@@ -106,6 +106,7 @@
 /************************************************************************/ 
 uint8 Command_Sent[100];
 uint8 Command_Length_Sent;
+uint8 Checkin_Detect_Status = FALSE;
 void Extract_Received_Lora_Response(void);
  
  //This function is Interrupt routine for detecting OBD Plugin and Out
@@ -165,6 +166,75 @@
 }
 */
 
+typedef struct {
+    uint8_t Queue_Msg_Type;   /* A counter value               */
+} message_type;
+
+MemoryPool<message_type, 16> mpool;
+Queue<message_type, 16> queue;   
+Thread OBD_Thread;
+Thread Accelerometer_Thread;
+
+#define OBD_MSG_TYPE                    0x01
+#define ACCELEROMETER_MSG_TYPE          0x02
+
+/* Send Thread */
+void OBD_Process_Thread (void) {
+    while (true) {
+        //DEBUG_UART.printf("\n OBD Message start ");
+        message_type message = mpool.alloc();
+        message->Queue_Msg_Type = (uint8_t) OBD_MSG_TYPE; 
+        queue.put(message);
+        //DEBUG_UART.printf("\n OBD Message Type Enqueued: \n");//, message->Queue_Msg_Type);
+        //Thread::wait(3000);
+        Thread::yield();
+    }
+}
+
+/* Send Thread */
+void Accelerometer_Process_Thread (void) {
+    while (true) {
+        //DEBUG_UART.printf("\n Accelerometer Message start: ");
+        message_type message = mpool.alloc();
+        message->Queue_Msg_Type = (uint8_t) ACCELEROMETER_MSG_TYPE; 
+        queue.put(message);
+        //DEBUG_UART.printf("\n Accelerometer Message Type Enqueued: \n");//, message->Queue_Msg_Type);
+        //Thread::wait(5000);
+        Thread::yield();
+    }
+}
+
+int main (void) {
+    DEBUG_UART.baud(115200);    
+    //OBD_Thread.set_priority(osPriorityLow);
+    OBD_Thread.start(OBD_Process_Thread);
+    //DEBUG_UART.printf("\n main %d", __LINE__);
+    //Accelerometer_Thread.set_priority(osPriorityAboveNormal);
+    //DEBUG_UART.printf("\n main %d", __LINE__);
+    Accelerometer_Thread.start(Accelerometer_Process_Thread);
+    //DEBUG_UART.printf("\n main %d", __LINE__);
+#if 1    
+    while (true) {
+        DEBUG_UART.printf("\n main %d", __LINE__);
+         Thread::yield();
+        /*
+        osEvent evt = queue.get();
+        if (evt.status == osEventMessage) {
+            message_type *message = (message_type*)evt.value.p;
+            DEBUG_UART.printf("\nMessage Type Dequeued %d"   , message->Queue_Msg_Type);
+            mpool.free(message);
+        }*/
+    }
+#endif
+    OBD_Thread.join();  
+    Accelerometer_Thread.join(); 
+}
+
+
+
+
+
+/*
 int main()
 {
     DEBUG_UART.baud(115200);
@@ -181,10 +251,10 @@
     //led2_thread is executing concurrently with main at this point
     CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt);
     inactivity.rise(interrupt_activity_inactivity); // Attach the address of interrupt_activity_inactivity function to rising edge
-    Initialize_Beacon_Module();
+    //Initialize_Beacon_Module();
     Lora_Periodic_Packet_Sending(); //Infinite loop for sending and receiving lora response, no return from here
 }
-
+*/
  //Functiont to be called when Interrupt is genearted for motion sensing, checkin
  void Initialize_Packets_Sent_Count(void)
  {
@@ -192,6 +262,7 @@
     CheckIN_Packet_Sent_Count = 0x00;
  } 
  
+ uint8 Status_Packet_Wait_Count = 0;
 void Lora_Periodic_Packet_Sending() 
 {
     DEBUG_UART.printf("Periodic packet sending intiialized");
@@ -201,16 +272,30 @@
     {
         if(Packet_Type_To_Send == HEARTBEAT_TYPE_PACKET)    //check if packet to be sent is Heartbeat packet
         {
-            if(Send_Lora_Packet_Flag)       //Check if packet sending isd enabled, Packet should be sent only when enabled after timeout period
+            if(Send_Lora_Packet_Flag)       //Check if packet sending is enabled, Packet should be sent only when enabled after timeout period
             {
-                
-                Send_HeartBeat_Packet();                            //call function to send heartbeat packet
-                DEBUG_UART.printf("Sent HeartBeat Packet");
-                AT_Response_Receive_Status = FAILURE;
-                while(AT_Response_Receive_Status)
-                    Get_Lora_Response();
-                Send_Lora_Packet_Flag = FALSE;
-                DEBUG_UART.printf("Heartbeat Packet Response Received");
+                Status_Packet_Wait_Count++;
+                if(Status_Packet_Wait_Count < 5)
+                {
+                    Send_HeartBeat_Packet();                            //call function to send heartbeat packet
+                    DEBUG_UART.printf("Sent HeartBeat Packet");
+                    AT_Response_Receive_Status = FAILURE;
+                    while(AT_Response_Receive_Status)
+                        Get_Lora_Response();
+                    Send_Lora_Packet_Flag = FALSE;
+                    DEBUG_UART.printf("Heartbeat Packet Response Received");
+                }
+                else
+                {
+                    Status_Packet_Wait_Count = 0;
+                    //Send_Vehicle_Status_Packet();                            //call function to send heartbeat packet
+                    DEBUG_UART.printf("Sent Status Packet");
+                    AT_Response_Receive_Status = FAILURE;
+                    while(AT_Response_Receive_Status)
+                        Get_Lora_Response();
+                    Send_Lora_Packet_Flag = FALSE;
+                    DEBUG_UART.printf("Status Packet Response Received");
+                }
             }
         }
         else if(Packet_Type_To_Send == MOTION_TYPE_PACKET)      //check if packet to be sent is motion packet
@@ -261,6 +346,19 @@
             Enable_CheckIN_Packet_Sending();
             //Get_Acceleration_Type();
             OBD_PlugInOut_IOC_Status = FALSE;
+            Checkin_Detect_Status ^= 0x01;
+            if(Checkin_Detect_Status)   //OBD Plugin detected
+            {
+                wait(1); //wait for 1sec to avoid detecting plugin debounce
+                CheckIn_Interrupt.fall(&Handle_CheckIn_Interrupt);  //Change interrupt on change to falling type to detect plugout
+                //write code to enable motion interrupt
+            }
+            else    //OBD Plugout detecetd
+            {
+                CheckIn_Interrupt.rise(&Handle_CheckIn_Interrupt);   //Change interrupt on change to rising type to detect plugin
+                wait(1); //wait for 1sec to avoid detecting plugin debounce
+                //write code to disable motion interrupt
+            }
         }
         if(Motion_Detect_Status)    //Check if Accelerometer Interrupt is generated
         {
@@ -283,16 +381,8 @@
 //This function is used to enable checkin packet sending once OBD device is plugged in
 void Enable_CheckIN_Packet_Sending() 
 {
-    Packet_Type_To_Send = CHECKIN_TYPE_PACKET;          //whenever acceleration is detected change the packet type to be sent to motion type
-    OBD_Plugin_Detected = TRUE;
-    Lora_Packet_Sending_Ticker.detach();                //destroy ticker                                  
-    Lora_Packet_Sending_Ticker.attach(&flip_Packet_Sending_Flag, 5.0); //create new ticker
-    Send_Lora_Packet_Flag = 1;                          //Set Counter so that packet is sent instantly as soon as checkin interrupt is detected
-}
-
-static void Process_Lora_Command_Received(uint8* Command_Received,uint8 Command_Length)
-{
-    if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE
+   // Enable_CheckIN_Packet_Sending = TRUE;  //set status to true
+   /* if(Command_Received[0] == 0xFE) //check start of frame of packet received. every packet must start with 0xFE
     {
         if(Calculate_Lora_Frame_FCS((Command_Received + 1),(Command_Length + 1)) == (Command_Received[Command_Length + 2]))  //Check for packet inegrity by checking FCS
         {
@@ -301,5 +391,5 @@
                 Process_Beacon_Command_Received((Command_Received + 2));
             }
         }
-    }
+    }*/
 }