1 /******************************************************************************
2  *
3  *  Copyright (C) 2018 ST Microelectronics S.A.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *
18  ******************************************************************************/
19 #include "Utils.h"
20 
21 #include <stdio.h>
22 #include "android_logmsg.h"
23 
24 /*******************************************************************************
25 **
26 ** Function        Utils_charArrayToHexString
27 **
28 ** Description     Converts the given char array into its HEX-based string
29 **                 representation.
30 **
31 ** Parameters      array     - char array to be converted.
32 **                 length    - length of the char array.
33 **                 hexString - Output hex string buffer.
34 **
35 ** Returns        0 on successful conversion, -1 otherwise.
36 **
37 *******************************************************************************/
Utils_charArrayToHexString(char * array,int length,char * hexString)38 int Utils_charArrayToHexString(char* array, int length, char* hexString) {
39   char* ptr = hexString;
40   int i;
41   for (i = 0; i < length; i++) {
42     ptr += sprintf(ptr, "%02X ", array[i]);
43   }
44   return 0;
45 }
46 
47 /*******************************************************************************
48 **
49 ** Function        Utils_getElapsedTimeInMs
50 **
51 ** Description     Returns the difference of time (in ms) between t1 and t2.
52 **
53 ** Parameters      t1  - initial time.
54 **                 t2  - final time.
55 **
56 ** Returns       The difference t2 - t1 in ms.
57 **
58 *******************************************************************************/
Utils_getElapsedTimeInMs(struct timeval t1,struct timeval t2)59 int Utils_getElapsedTimeInMs(struct timeval t1, struct timeval t2) {
60   return (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000;
61 }
62 
63 /*******************************************************************************
64 **
65 ** Function        Utils_getElapsedTimeInUs
66 **
67 ** Description     Returns the difference of time (in Us) between t1 and t2.
68 **
69 ** Parameters      t1  - initial time.
70 **                 t2  - final time.
71 **
72 ** Returns       The difference t2 - t1 in ms.
73 **
74 *******************************************************************************/
Utils_getElapsedTimeInUs(struct timeval t1,struct timeval t2)75 int Utils_getElapsedTimeInUs(struct timeval t1, struct timeval t2) {
76   return (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);
77 }
78 
79 /*******************************************************************************
80 **
81 ** Function        Utils_printCurrentTime
82 **
83 ** Description     Prints current time to standard log.
84 **
85 ** Parameters      prefix- The prefix to be printed before the time.
86 **
87 ** Returns         void
88 **
89 *******************************************************************************/
Utils_printCurrentTime(char * prefix)90 void Utils_printCurrentTime(char* prefix) {
91   struct timeval currentTime;
92   gettimeofday(&currentTime, 0);
93   STLOG_HAL_V("SpiTiming:  %s: %ld,%ld", prefix, currentTime.tv_sec,
94               currentTime.tv_usec);
95 }
96