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, committed 2020-03-20
- Comitter:
- vithyat
- Date:
- Fri Mar 20 20:15:18 2020 +0000
- Parent:
- 1:0c589850480e
- Commit message:
- Update to work with P2Scan runtime
Changed in this revision
diff -r 0c589850480e -r 990c985a69ae main.cpp --- 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); } }
diff -r 0c589850480e -r 990c985a69ae update_certificate.pem --- a/update_certificate.pem Wed Nov 20 18:58:49 2019 +0000 +++ b/update_certificate.pem Fri Mar 20 20:15:18 2020 +0000 @@ -1,9 +1,9 @@ -----BEGIN CERTIFICATE----- -MIIBTDCB9KADAgECAhRfg5JQzeSpDAMd0aTujOj2HPr3KTAKBggqhkjOPQQDAjAU -MRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMTkxMTIwMTg0ODE2WhcNMjAxMTIwMDUw +MIIBTDCB9KADAgECAhSTQyYDsCEkNJj27mizN3KgXDT3jjAKBggqhkjOPQQDAjAU +MRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMjAwMzIwMTkxMzI5WhcNMjEwMzIwMDQw MDAwWjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwWTATBgcqhkjOPQIBBggqhkjOPQMB -BwNCAAR6ggbsgMaGNjSsONULOg/VndJOAv5KYh3PTMIeppOT9JG7uV+pHLu7dMVm -F3C3O7UHmMQOjpliQeMSTHBqhPLcoyQwIjALBgNVHQ8EBAMCB4AwEwYDVR0lBAww -CgYIKwYBBQUHAwMwCgYIKoZIzj0EAwIDRwAwRAIgrUKJQtJgAFlNfzGZwq7qBq44 -tAtMg7JyNJ4s/l0SGp4CIPqWr/iiMjbBSZTMCXd4X7bdrxXL3Yt+eWovSnQhcyRr +BwNCAAT95GWPzA0zgSNg56OT/R5Mu5hYtjgDCkyU1ng6mPWSoRzFnrHjZ5V9mKLe +eZ6rXPGJmup8gGMhdPcYArk/uncUoyQwIjALBgNVHQ8EBAMCB4AwEwYDVR0lBAww +CgYIKwYBBQUHAwMwCgYIKoZIzj0EAwIDRwAwRAIgs9moQ3JP24KDp6TwRU7kaqS1 +U0lt9Saq8f/GDPIm1/MCIDhnyyOICLdubadgUqwV4Q66nIOTYpD4wqoQi/ycJJsU -----END CERTIFICATE-----
diff -r 0c589850480e -r 990c985a69ae update_default_resources.c --- a/update_default_resources.c Wed Nov 20 18:58:49 2019 +0000 +++ b/update_default_resources.c Fri Mar 20 20:15:18 2020 +0000 @@ -12,15 +12,15 @@ const uint16_t arm_uc_vendor_id_size = sizeof(arm_uc_vendor_id); const uint8_t arm_uc_class_id[] = { - 0x95, 0x9e, 0xdf, 0x33, 0xe0, 0x68, 0x55, 0xc2, 0x80, 0x7f, 0xb1, 0x6b, 0xa9, 0x8d, 0x29, 0x6a + 0x91, 0x3a, 0x74, 0xbf, 0xb5, 0x47, 0x50, 0x3d, 0xbc, 0xb5, 0xa0, 0x44, 0xf9, 0x98, 0x5e, 0x2f }; const uint16_t arm_uc_class_id_size = sizeof(arm_uc_class_id); #endif #ifdef MBED_CLOUD_DEV_UPDATE_CERT const uint8_t arm_uc_default_fingerprint[] = { - 0xc5, 0x76, 0xdf, 0x70, 0xe5, 0x72, 0xfe, 0x9f, 0xdc, 0x4a, 0xba, 0x88, 0x29, 0x92, 0xef, 0x4d, - 0x42, 0x1e, 0x7a, 0x8c, 0x47, 0xa0, 0x4f, 0xb3, 0x6e, 0xe0, 0x9d, 0xcf, 0x54, 0xc1, 0xb2, 0x64 + 0x86, 0x82, 0x1c, 0x53, 0x9c, 0x5f, 0x96, 0x9c, 0x6a, 0xac, 0x39, 0xb3, 0xb8, 0x13, 0xd5, 0xb2, + 0xfe, 0x21, 0xe7, 0x66, 0xb4, 0xa4, 0xba, 0xd5, 0x92, 0x66, 0x5b, 0xb4, 0x40, 0xab, 0x1a, 0x96 }; const uint16_t arm_uc_default_fingerprint_size = sizeof(arm_uc_default_fingerprint); @@ -31,27 +31,27 @@ sizeof(arm_uc_default_subject_key_identifier); const uint8_t arm_uc_default_certificate[] = { - 0x30, 0x82, 0x01, 0x4c, 0x30, 0x81, 0xf4, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x5f, 0x83, - 0x92, 0x50, 0xcd, 0xe4, 0xa9, 0x0c, 0x03, 0x1d, 0xd1, 0xa4, 0xee, 0x8c, 0xe8, 0xf6, 0x1c, 0xfa, - 0xf7, 0x29, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x14, + 0x30, 0x82, 0x01, 0x4c, 0x30, 0x81, 0xf4, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x93, 0x43, + 0x26, 0x03, 0xb0, 0x21, 0x24, 0x34, 0x98, 0xf6, 0xee, 0x68, 0xb3, 0x37, 0x72, 0xa0, 0x5c, 0x34, + 0xf7, 0x8e, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, - 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x39, 0x31, 0x31, 0x32, 0x30, 0x31, 0x38, - 0x34, 0x38, 0x31, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x30, 0x31, 0x31, 0x32, 0x30, 0x30, 0x35, 0x30, + 0x68, 0x6f, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x33, 0x32, 0x30, 0x31, 0x39, + 0x31, 0x33, 0x32, 0x39, 0x5a, 0x17, 0x0d, 0x32, 0x31, 0x30, 0x33, 0x32, 0x30, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x14, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, - 0x07, 0x03, 0x42, 0x00, 0x04, 0x7a, 0x82, 0x06, 0xec, 0x80, 0xc6, 0x86, 0x36, 0x34, 0xac, 0x38, - 0xd5, 0x0b, 0x3a, 0x0f, 0xd5, 0x9d, 0xd2, 0x4e, 0x02, 0xfe, 0x4a, 0x62, 0x1d, 0xcf, 0x4c, 0xc2, - 0x1e, 0xa6, 0x93, 0x93, 0xf4, 0x91, 0xbb, 0xb9, 0x5f, 0xa9, 0x1c, 0xbb, 0xbb, 0x74, 0xc5, 0x66, - 0x17, 0x70, 0xb7, 0x3b, 0xb5, 0x07, 0x98, 0xc4, 0x0e, 0x8e, 0x99, 0x62, 0x41, 0xe3, 0x12, 0x4c, - 0x70, 0x6a, 0x84, 0xf2, 0xdc, 0xa3, 0x24, 0x30, 0x22, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, + 0x07, 0x03, 0x42, 0x00, 0x04, 0xfd, 0xe4, 0x65, 0x8f, 0xcc, 0x0d, 0x33, 0x81, 0x23, 0x60, 0xe7, + 0xa3, 0x93, 0xfd, 0x1e, 0x4c, 0xbb, 0x98, 0x58, 0xb6, 0x38, 0x03, 0x0a, 0x4c, 0x94, 0xd6, 0x78, + 0x3a, 0x98, 0xf5, 0x92, 0xa1, 0x1c, 0xc5, 0x9e, 0xb1, 0xe3, 0x67, 0x95, 0x7d, 0x98, 0xa2, 0xde, + 0x79, 0x9e, 0xab, 0x5c, 0xf1, 0x89, 0x9a, 0xea, 0x7c, 0x80, 0x63, 0x21, 0x74, 0xf7, 0x18, 0x02, + 0xb9, 0x3f, 0xba, 0x77, 0x14, 0xa3, 0x24, 0x30, 0x22, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x07, 0x80, 0x30, 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x03, 0x30, 0x0a, 0x06, 0x08, 0x2a, - 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0xad, 0x42, - 0x89, 0x42, 0xd2, 0x60, 0x00, 0x59, 0x4d, 0x7f, 0x31, 0x99, 0xc2, 0xae, 0xea, 0x06, 0xae, 0x38, - 0xb4, 0x0b, 0x4c, 0x83, 0xb2, 0x72, 0x34, 0x9e, 0x2c, 0xfe, 0x5d, 0x12, 0x1a, 0x9e, 0x02, 0x20, - 0xfa, 0x96, 0xaf, 0xf8, 0xa2, 0x32, 0x36, 0xc1, 0x49, 0x94, 0xcc, 0x09, 0x77, 0x78, 0x5f, 0xb6, - 0xdd, 0xaf, 0x15, 0xcb, 0xdd, 0x8b, 0x7e, 0x79, 0x6a, 0x2f, 0x4a, 0x74, 0x21, 0x73, 0x24, 0x6b + 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0xb3, 0xd9, + 0xa8, 0x43, 0x72, 0x4f, 0xdb, 0x82, 0x83, 0xa7, 0xa4, 0xf0, 0x45, 0x4e, 0xe4, 0x6a, 0xa4, 0xb5, + 0x53, 0x49, 0x6d, 0xf5, 0x26, 0xaa, 0xf1, 0xff, 0xc6, 0x0c, 0xf2, 0x26, 0xd7, 0xf3, 0x02, 0x20, + 0x38, 0x67, 0xcb, 0x23, 0x88, 0x08, 0xb7, 0x6e, 0x6d, 0xa7, 0x60, 0x52, 0xac, 0x15, 0xe1, 0x0e, + 0xba, 0x9c, 0x83, 0x93, 0x62, 0x90, 0xf8, 0xc2, 0xaa, 0x10, 0x8b, 0xfc, 0x9c, 0x24, 0x9b, 0x14 }; const uint16_t arm_uc_default_certificate_size = sizeof(arm_uc_default_certificate); #endif