This program collects raw time series data from the ADC using the NXP board that will later be post processed by PFP Cyber-security cloud base machine learning engine to determine the state of the device.

Dependencies:   FXAS21002 FXOS8700Q

Revision:
2:990c985a69ae
Parent:
1:0c589850480e
--- a/main.cpp	Wed Nov 20 18:58:49 2019 +0000
+++ b/main.cpp	Fri Mar 20 20:15:18 2020 +0000
@@ -22,11 +22,11 @@
 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi.
 NetworkInterface *net = NetworkInterface::get_default_instance();
 
-AnalogIn ain(A1);
+AnalogIn ain(A0);
 
 ///////////////////////////////////////////////////////////////////////////////////////
 ///
-///                        PFP Main code start 
+///                        PFP Main code start
 ///
 //////////////////////////////////////////////////////////////////////////////////////
 uint8_t PFP_REGISTER_DIGITIZER = 87;
@@ -189,6 +189,7 @@
     srv.bind(net->get_ip_address(), 7001);
 
     long traceTrack = 0;
+    float sleepCore = 0.001;
 
 
     //srv.set_blocking(false);
@@ -210,11 +211,16 @@
                 int16_t MAX_TRANSMIT_DATA = 500;
                 int16_t data[MAX_TRANSMIT_DATA];
                 traceTrack = 0;
+
+                bool debug = false;
+
                 while(1) {
 
                     ////////////////
-                    //  int len = read(sockfd, bytes, sizeof(bytes));
                     int len = clt_sock.recv(bytes, MAX_LEN);
+                    if(debug) {
+                        printf("====================> len = %i <======================\n",len);
+                    }
 
                     if (len < 1) {
                         printf("Connection is closed....\n");
@@ -223,6 +229,7 @@
 
                     int commandType = 0;
                     int commandLength = 0;
+                    int indexOffset = 0;
 
                     int8_t *sendBytes = NULL;
 
@@ -239,17 +246,40 @@
                             deviceData.digitizer = bytesToInt(bytes[8], bytes[9], bytes[10],
                                                               bytes[11]);
 
+                            //  send_PFP_REGISTER_DIGITIZER(clt_sock);
+
                         } else if (commandType == PFP_CLK_FREQ) {
                             deviceData.sampleRate = bytesToInt(bytes[8], bytes[9],
                                                                bytes[10], bytes[11]);
+                            //  send_PFP_CLK_FREQ(clt_sock);
                         } else if (commandType == PFP_ADC_GET_RAW) {
                             deviceData.channel = bytesToInt(bytes[8], bytes[9], bytes[10],
                                                             bytes[11]);
                         }
+                        if(debug) {
+                            printf("====================> len 12 commandType= %i <======================\n",commandType);
+                            printf("====================> len 12 commandLength= %i <======================\n",commandLength);
+                        }
 
                     } else {
                         commandType = bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
                         commandLength = bytesToInt(bytes[4], bytes[5], bytes[6], bytes[7]);
+                        if (commandLength == 8) {
+                            if ((len == 60) || (len == 72)) {
+                                commandType =bytesToInt(bytes[8], bytes[9], bytes[10], bytes[11]);
+                                commandLength = bytesToInt(bytes[12], bytes[13], bytes[14], bytes[15]);
+                                indexOffset = 8;
+                                if(debug) {
+                                    printf("====================> inside commandType= %i <======================\n",commandType);
+                                    printf("====================> inside commandLength= %i <======================\n",commandLength);
+                                }
+                            }
+                        } else {
+                            if(debug) {
+                                printf("====================> outside commandType= %i <======================\n",commandType);
+                                printf("====================> outside commandLength= %i <======================\n",commandLength);
+                            }
+                        }
                     }
 
                     // Got command form client. Send back header
@@ -258,85 +288,127 @@
 
                         if (sendBytes != NULL) {
                             clt_sock.send(sendBytes,64);
-                            wait(0.05);
+                            wait(sleepCore);
                         }
-                    } else if (commandType == PFP_REGISTER_DIGITIZER) {
+                    }
+
+                    if (commandType == PFP_REGISTER_DIGITIZER) {
                         sendBytes = pfp_emon_create_ack_for_client(commandType, 0);
 
                         if (sendBytes != NULL) {
                             clt_sock.send(sendBytes,64);
-                            wait(0.05);
+                            wait(sleepCore);
                         }
-                    } else if (commandType == PFP_TRIG_CONFIG) {
+                    }
+                    if (commandType == PFP_TRIG_CONFIG) {
 
                         if ((commandLength > 12) && (commandLength < 1000)) {
-                            deviceData.channel = bytesToInt(bytes[8], bytes[9], bytes[10],
-                                                            bytes[11]);
-                            deviceData.traceLength = bytesToInt(bytes[12], bytes[13],
-                                                                bytes[14], bytes[15]);
+
+                            deviceData.channel = bytesToInt(bytes[indexOffset+8], bytes[indexOffset+9], bytes[indexOffset+10],
+                                                            bytes[indexOffset+11]);
+                            deviceData.traceLength = bytesToInt(bytes[indexOffset+12], bytes[indexOffset+13],
+                                                                bytes[indexOffset+14], bytes[indexOffset+15]);
 
-                            deviceData.trigMode = bytesToInt(bytes[16], bytes[17],
-                                                             bytes[18], bytes[19]);
-                            deviceData.trigSource = bytesToInt(bytes[20], bytes[21],
-                                                               bytes[22], bytes[23]);
+                            deviceData.trigMode = bytesToInt(bytes[indexOffset+16], bytes[indexOffset+17],
+                                                             bytes[indexOffset+18], bytes[indexOffset+19]);
+                            deviceData.trigSource = bytesToInt(bytes[indexOffset+20], bytes[indexOffset+21],
+                                                               bytes[indexOffset+22], bytes[indexOffset+23]);
 
-                            deviceData.trigLevel = bytesToFloat(bytes[24], bytes[25],
-                                                                bytes[26], bytes[27]);
-                            deviceData.trigHyst = bytesToFloat(bytes[28], bytes[29],
-                                                               bytes[30], bytes[31]);
+                            deviceData.trigLevel = bytesToFloat(bytes[indexOffset+24], bytes[indexOffset+25],
+                                                                bytes[indexOffset+26], bytes[indexOffset+27]);
+                            deviceData.trigHyst = bytesToFloat(bytes[indexOffset+28], bytes[indexOffset+29],
+                                                               bytes[indexOffset+30], bytes[indexOffset+31]);
 
-                            deviceData.trigPercentage = bytesToInt(bytes[32], bytes[33],
-                                                                   bytes[34], bytes[35]);
-                            deviceData.gain = bytesToInt(bytes[36], bytes[37], bytes[38],
-                                                         bytes[39]);
+                            deviceData.trigPercentage = bytesToInt(bytes[indexOffset+32], bytes[indexOffset+33],
+                                                                   bytes[indexOffset+34], bytes[indexOffset+35]);
+                            deviceData.gain = bytesToInt(bytes[indexOffset+36], bytes[indexOffset+37], bytes[indexOffset+38],
+                                                         bytes[indexOffset+39]);
+                            if(debug) {
+                                printf("channel is %i\n", deviceData.channel);
+                                printf("traceLength is %i\n", deviceData.traceLength);
+                                printf("trigMode is %f\n", deviceData.trigLevel);
+                                printf("trigSource is %i\n", deviceData.trigSource);
+                                printf("trigLevel is %f\n", deviceData.trigLevel);
+                                printf("trigHyst is %f\n", deviceData.trigHyst);
+                                printf("trigPercentage is %i\n", deviceData.trigPercentage);
+                                printf("gain is %i\n", deviceData.gain);
+                                printf("sample rate is %i\n", deviceData.sampleRate);
+                                printf("gain is %i\n", deviceData.gain);
+                            }
 
-                            printf("trigSource is %i\n", deviceData.trigSource);
-                            printf("channel is %i\n", deviceData.channel);
-                            printf("trigLevel is %f\n", deviceData.trigLevel);
-                            printf("traceLength is %i\n", deviceData.traceLength);
-                            printf("sample rate is %i\n", deviceData.sampleRate);
-                            printf("trigHyst is %i\n", deviceData.trigHyst);
-
-                            printf("trigPercentage is %i\n", deviceData.trigPercentage);
-                            printf("gain is %i\n", deviceData.gain);
-
+                            commandType = PFP_TRIG_RECEIVED;
                             sendBytes = pfp_emon_create_ack_for_client(commandType,
                                         deviceData.traceLength * 2);
 
                             if (sendBytes != NULL) {
                                 clt_sock.send(sendBytes,64);
-                                wait(0.05);
+                            }
+
+                            int numOfInteration = (len - (indexOffset+40))/4;
+
+                            for(int i=0; i<numOfInteration; i++) {
+                                int index2 = i*4;
+                                int b0 = indexOffset+40+index2;
+                                int b1 = indexOffset+41+index2;
+                                int b2 = indexOffset+42+index2;
+                                int b3 = indexOffset+43+index2;
+                                int v = bytesToInt(bytes[b0], bytes[b1], bytes[b2],bytes[b3]);
+                                if(debug) {
+                                    printf("Trailing value %i\n", v);
+                                }
                             }
-                        }
+
+                            if(numOfInteration==6) {
+
+                                commandType = bytesToInt(bytes[indexOffset+40], bytes[indexOffset+41], bytes[indexOffset+42],bytes[indexOffset+43]);
+                                commandLength = bytesToInt(bytes[indexOffset+44], bytes[indexOffset+45], bytes[indexOffset+46],bytes[indexOffset+47]);
+                                int sampleRate = bytesToInt(bytes[indexOffset+48], bytes[indexOffset+49], bytes[indexOffset+50],bytes[indexOffset+51]);
+                                deviceData.sampleRate = sampleRate;
+
+                                if (commandType == PFP_CLK_FREQ) {
+                                    sendBytes = pfp_emon_create_ack_for_client(commandType, 0);
+                                    if (sendBytes != NULL) {
+                                        clt_sock.send(sendBytes,64);
+                                    }
+                                }
+                                wait(sleepCore);
+
+                                commandType = bytesToInt(bytes[indexOffset+52], bytes[indexOffset+53], bytes[indexOffset+54],bytes[indexOffset+55]);
+                                commandLength = bytesToInt(bytes[indexOffset+56], bytes[indexOffset+57], bytes[indexOffset+58],bytes[indexOffset+59]);
+                                //  int sampleRate = bytesToInt(bytes[indexOffset+40], bytes[indexOffset+41], bytes[indexOffset+42],bytes[indexOffset+43]);
+                            }
 
 
-                    } else if (commandType == PFP_CLK_FREQ) {
+                        }
+                    }
+                    if (commandType == PFP_CLK_FREQ) {
                         sendBytes = pfp_emon_create_ack_for_client(commandType, 0);
                         if (sendBytes != NULL) {
                             clt_sock.send(sendBytes,64);
-                            wait(0.05);
+                            wait(sleepCore);
+                        }
+
+                        if(len==24) {
+                            commandType = PFP_ADC_GET_RAW;
                         }
-                    } else if (commandType == PFP_ADC_GET_RAW) {
+
+                    }
 
+
+                    if (commandType == PFP_ADC_GET_RAW) {
                         sendBytes = pfp_emon_create_ack_for_client(PFP_TRIG_RECEIVED,
                                     deviceData.traceLength * 2);
 
                         if (sendBytes != NULL) {
                             clt_sock.send(sendBytes,64);
-                            wait(0.05);
                         }
 
-                        wait(0.05);
-
                         sendBytes = pfp_emon_create_ack_for_client(commandType,
                                     deviceData.traceLength * 2);
                         if (sendBytes != NULL) {
                             clt_sock.send(sendBytes,64);
-                            wait(0.05);
                         }
 
-                        wait(0.05);
-
                         if (deviceData.traceLength != rawDataLen) {
                             printf("Data size change to %i===============>\n",rawDataLen);
                             free(rawData);
@@ -345,33 +417,19 @@
                         }
 
                         // Store raw data
-                        printf("Populate sensor data to %i===============>\n",rawDataLen);
-//                        for (int i = 0; i < rawDataLen; i++) {
-//                            rawData[i] = ain.read_u16();
-//                        }        
-                                        
-                        float max = -32768;
-                        uint16_t val=0;
-                        
-                        // find max
+
+                        int val=0;
+
                         for (int i = 0; i < rawDataLen; i++) {
-                            val = (ain.read_u16()-32768);
+                            val = ain.read_u16() - 32768;
+                            if(val>32762) {
+                                val = 32762;
+                            } else if(val<-32762) {
+                                val = -32762;
+                            }
+
                             rawData[i] =val;
-                            if(val>max){
-                                max = val;
-                            }
                         }
-                        
-                        float scaling = 32762/abs(max);
-                        printf("scaling value %f===============>\n",scaling);
-                        for (int i = 0; i < rawDataLen; i++) {
-                            rawData[i] =(int16_t)(rawData[i]*scaling);                        
-                        }
-                        
-                        
-                        ///////////////////////////////////////////////////////////
-                        
-                        
 
                         int num = rawDataLen/MAX_TRANSMIT_DATA;
                         int startIndex = 0;
@@ -381,12 +439,10 @@
                                 data[i] = rawData[i + startIndex];
                             }
                             clt_sock.send(data,MAX_TRANSMIT_DATA*sizeof(int16_t));
-                            //printf("Sending batch %i of %i\n",k,num);
-                            wait(0.04);
+                            wait(sleepCore);
                         }
 
                         int leftOver = rawDataLen - num*MAX_TRANSMIT_DATA;
-                        //printf("LeftOver is %i\n",leftOver);
 
                         if(leftOver>0) {
                             startIndex = num*MAX_TRANSMIT_DATA;
@@ -396,14 +452,14 @@
                                 data[i] = rawData[j];
                             }
                             clt_sock.send(data,leftOver * sizeof(int16_t));
-                            //printf("Sending left over bytes %i\n",leftOver);
-                            wait(0.04);
                         }
                         traceTrack++;
                         if(traceTrack>100000000) {
                             traceTrack = 0;
                         }
-                        printf("<================== Trace Count is %ld ===============>\n",traceTrack);
+                        if(debug) {
+                            printf("<================== Trace Count is %ld ===============>\n",traceTrack);
+                        }
                     }
                 }
 
@@ -415,7 +471,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////
 ///
-///                        PFP Main code end 
+///                        PFP Main code end
 ///
 //////////////////////////////////////////////////////////////////////////////////////
 
@@ -432,7 +488,7 @@
     Thread thread(startEmonThread);
 
     while (true) {
-        wait(0.5);
+        wait(0.001);
     }
 }