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
Diff: main.cpp
- Revision:
- 1:0c589850480e
- Parent:
- 0:977e87915078
- Child:
- 2:990c985a69ae
diff -r 977e87915078 -r 0c589850480e main.cpp --- a/main.cpp Wed Aug 28 19:24:56 2019 +0000 +++ b/main.cpp Wed Nov 20 18:58:49 2019 +0000 @@ -24,14 +24,18 @@ AnalogIn ain(A1); +/////////////////////////////////////////////////////////////////////////////////////// +/// +/// PFP Main code start +/// +////////////////////////////////////////////////////////////////////////////////////// uint8_t PFP_REGISTER_DIGITIZER = 87; -uint8_t PFP_CLK_FREQ = 11; +uint8_t PFP_CLK_FREQ = 11; uint8_t PFP_ADC_GET_RAW = 3; uint8_t PFP_ADC_INIT = 0; uint8_t PFP_TRIG_CONFIG = 26; - +uint8_t PFP_TRIG_RECEIVED = 88; -/////////////////////////////////////////////////////////////////////////////////////// struct DeviceData { int channel; @@ -78,7 +82,8 @@ int8_t * intToBytes(int value) { - static int8_t b[4]; + int8_t *b = (int8_t*) calloc(4, sizeof(int8_t)); + b[0] = ((int8_t) (value >> 0)); b[1] = ((int8_t) (value >> 8)); b[2] = ((int8_t) (value >> 16)); @@ -100,13 +105,14 @@ return b; } -int8_t * pfp_emon_create_ack_for_client(int commandType, long numberOfBytes) +int8_t * pfp_emon_create_ack_for_client(int commandType, int numberOfBytes) { - static int8_t b[24]; + static int8_t b[64]; - int8_t *totalBytes = longToBytes(numberOfBytes); + int8_t *totalBytes = intToBytes(numberOfBytes); int8_t *successBytes = intToBytes(3); int8_t *returnBytes = intToBytes(commandType); + int8_t *totaTrace = intToBytes(numberOfBytes / 2); // EMON HEADER b[0] = 69; @@ -119,10 +125,12 @@ b[5] = totalBytes[1]; b[6] = totalBytes[2]; b[7] = totalBytes[3]; - b[8] = totalBytes[4]; - b[9] = totalBytes[5]; - b[10] = totalBytes[6]; - b[11] = totalBytes[7]; + + // ERROR + b[8] = 0; + b[9] = 0; + b[10] = 0; + b[11] = 0; // SKIP BYTES b[12] = 0; @@ -142,10 +150,28 @@ b[22] = returnBytes[2]; b[23] = returnBytes[3]; + // SKIP BYTES + b[24] = 0; + b[25] = 0; + b[26] = 0; + b[27] = 0; + + // TOTAL TRACE + b[28] = totaTrace[0]; + b[29] = totaTrace[1]; + b[30] = totaTrace[2]; + b[31] = totaTrace[3]; + + + free(totalBytes); + free(successBytes); + free(returnBytes); + free(totaTrace); + + return b; } -//////////////////////////////////////////////////////////////////////////////////// void startEmonThread(void const *args) { @@ -221,55 +247,96 @@ bytes[11]); } - } - - else if (len == 40) { - // Trig Configuration + } else { commandType = bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]); commandLength = bytesToInt(bytes[4], bytes[5], bytes[6], bytes[7]); - - deviceData.channel = bytesToInt(bytes[8], bytes[9], bytes[10], - bytes[11]); - deviceData.traceLength = bytesToInt(bytes[12], bytes[13], bytes[14], - bytes[15]); - - deviceData.trigMode = bytesToInt(bytes[16], bytes[17], bytes[18], - bytes[19]); - deviceData.trigSource = bytesToInt(bytes[20], bytes[21], bytes[22], - bytes[23]); - - deviceData.trigLevel = bytesToFloat(bytes[24], bytes[25], bytes[26], - bytes[27]); - deviceData.trigHyst = bytesToFloat(bytes[28], bytes[29], bytes[30], - bytes[31]); - - deviceData.trigPercentage = bytesToInt(bytes[32], bytes[33], - bytes[34], bytes[35]); - deviceData.gain = bytesToInt(bytes[36], bytes[37], bytes[38], - bytes[39]); } // Got command form client. Send back header if (commandType == PFP_ADC_INIT) { sendBytes = pfp_emon_create_ack_for_client(commandType, 0); + + if (sendBytes != NULL) { + clt_sock.send(sendBytes,64); + wait(0.05); + } } else 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); + } } else if (commandType == PFP_TRIG_CONFIG) { - sendBytes = pfp_emon_create_ack_for_client(commandType, - deviceData.traceLength * 2); + + 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.trigMode = bytesToInt(bytes[16], bytes[17], + bytes[18], bytes[19]); + deviceData.trigSource = bytesToInt(bytes[20], bytes[21], + bytes[22], bytes[23]); + + deviceData.trigLevel = bytesToFloat(bytes[24], bytes[25], + bytes[26], bytes[27]); + deviceData.trigHyst = bytesToFloat(bytes[28], bytes[29], + bytes[30], bytes[31]); + + deviceData.trigPercentage = bytesToInt(bytes[32], bytes[33], + bytes[34], bytes[35]); + deviceData.gain = bytesToInt(bytes[36], bytes[37], bytes[38], + bytes[39]); + + 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); + + sendBytes = pfp_emon_create_ack_for_client(commandType, + deviceData.traceLength * 2); + + if (sendBytes != NULL) { + clt_sock.send(sendBytes,64); + wait(0.05); + } + } + + } else 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); + } } else 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); + } - if (sendBytes != NULL) { - clt_sock.send(sendBytes,24); wait(0.05); - } - if (commandType == PFP_ADC_GET_RAW) { if (deviceData.traceLength != rawDataLen) { printf("Data size change to %i===============>\n",rawDataLen); free(rawData); @@ -279,9 +346,32 @@ // 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 for (int i = 0; i < rawDataLen; i++) { - rawData[i] = ain.read_u16(); + val = (ain.read_u16()-32768); + 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; @@ -310,13 +400,11 @@ wait(0.04); } traceTrack++; - if(traceTrack>100000000){ + if(traceTrack>100000000) { traceTrack = 0; } printf("<================== Trace Count is %ld ===============>\n",traceTrack); } - - /////////////// } free(rawData); @@ -324,7 +412,12 @@ } } } -/////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////////////// +/// +/// PFP Main code end +/// +////////////////////////////////////////////////////////////////////////////////////// int main(void) {